1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Collection types.

use crate::alloc::{Layout, LayoutErr};
use core::fmt::Display;
pub use liballoc::collections::{binary_heap, linked_list, vec_deque};

pub mod btree_map {
    pub use crate::btree::map::*;
}
pub mod btree_set {
    pub use crate::btree::set::*;
}

#[doc(no_inline)]
pub use self::{
    binary_heap::BinaryHeap,
    btree_map::BTreeMap,
    btree_set::BTreeSet,
    linked_list::LinkedList,
    vec_deque::VecDeque,
};

/// The error type for `try_reserve` methods.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum TryReserveError {
    /// Error due to the computed capacity exceeding the collection's maximum
    /// (usually `isize::MAX` bytes).
    CapacityOverflow,

    /// The memory allocator returned an error
    #[non_exhaustive]
    AllocError {
        /// The layout of allocation request that failed
        layout: Layout,
    },
}

impl From<LayoutErr> for TryReserveError {
    #[inline]
    fn from(_: LayoutErr) -> Self {
        Self::CapacityOverflow
    }
}

impl Display for TryReserveError {
    fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> Result<(), core::fmt::Error> {
        fmt.write_str("memory allocation failed")?;
        let reason = match &self {
            Self::CapacityOverflow => {
                " because the computed capacity exceeded the collection's maximum"
            }
            Self::AllocError { .. } => " because the memory allocator returned a error",
        };
        fmt.write_str(reason)
    }
}