Skip to main content

alloc/collections/
mod.rs

1//! Collection types.
2
3// Note: This module is also included in the alloctests crate using #[path] to
4// run the tests. See the comment there for an explanation why this is the case.
5
6#![stable(feature = "rust1", since = "1.0.0")]
7
8#[cfg(not(no_global_oom_handling))]
9pub mod binary_heap;
10#[cfg(not(no_global_oom_handling))]
11mod btree;
12#[cfg(not(no_global_oom_handling))]
13pub mod linked_list;
14pub(crate) mod retain;
15#[cfg(not(no_global_oom_handling))]
16pub mod vec_deque;
17
18#[cfg(not(no_global_oom_handling))]
19#[stable(feature = "rust1", since = "1.0.0")]
20pub mod btree_map {
21    //! An ordered map based on a B-Tree.
22    #[stable(feature = "rust1", since = "1.0.0")]
23    #[cfg(not(test))]
24    pub use super::btree::map::*;
25}
26
27#[cfg(not(no_global_oom_handling))]
28#[stable(feature = "rust1", since = "1.0.0")]
29pub mod btree_set {
30    //! An ordered set based on a B-Tree.
31    #[stable(feature = "rust1", since = "1.0.0")]
32    #[cfg(not(test))]
33    pub use super::btree::set::*;
34}
35
36#[cfg(not(test))]
37use core::fmt::Display;
38
39#[cfg(not(no_global_oom_handling))]
40#[stable(feature = "rust1", since = "1.0.0")]
41#[doc(no_inline)]
42#[cfg(not(test))]
43pub use binary_heap::BinaryHeap;
44#[cfg(not(no_global_oom_handling))]
45#[stable(feature = "rust1", since = "1.0.0")]
46#[doc(no_inline)]
47#[cfg(not(test))]
48pub use btree_map::BTreeMap;
49#[cfg(not(no_global_oom_handling))]
50#[stable(feature = "rust1", since = "1.0.0")]
51#[doc(no_inline)]
52#[cfg(not(test))]
53pub use btree_set::BTreeSet;
54#[cfg(not(no_global_oom_handling))]
55#[stable(feature = "rust1", since = "1.0.0")]
56#[doc(no_inline)]
57#[cfg(not(test))]
58pub use linked_list::LinkedList;
59#[cfg(not(no_global_oom_handling))]
60#[stable(feature = "rust1", since = "1.0.0")]
61#[doc(no_inline)]
62#[cfg(not(test))]
63pub use vec_deque::VecDeque;
64
65#[cfg(not(test))]
66use crate::alloc::{Layout, LayoutError};
67
68/// The error type for `try_reserve` methods.
69#[derive(#[automatically_derived]#[stable(feature = "try_reserve", since = "1.57.0")]
impl ::core::clone::Clone for TryReserveError {
    #[inline]
    #[rustc_trivial_field_reads]
    fn clone(&self) -> Self {
        TryReserveError { kind: ::core::clone::Clone::clone(&self.kind) }
    }
}Clone, #[automatically_derived]#[stable(feature = "try_reserve", since = "1.57.0")]
impl ::core::marker::StructuralPartialEq for TryReserveError { }
#[automatically_derived]#[stable(feature = "try_reserve", since = "1.57.0")]
impl ::core::cmp::PartialEq for TryReserveError {
    #[inline]
    fn eq(&self, other: &Self) -> bool { self.kind == other.kind }
}PartialEq, #[automatically_derived]#[stable(feature = "try_reserve", since = "1.57.0")]
impl ::core::cmp::Eq for TryReserveError {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<TryReserveErrorKind>;
    }
}Eq, #[automatically_derived]#[stable(feature = "try_reserve", since = "1.57.0")]
impl ::core::fmt::Debug for TryReserveError {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field1_finish(f,
            "TryReserveError kind", &&self.kind)
    }
}Debug)]
70#[stable(feature = "try_reserve", since = "1.57.0")]
71#[cfg(not(test))]
72pub struct TryReserveError {
73    kind: TryReserveErrorKind,
74}
75
76#[cfg(test)]
77pub use realalloc::collections::TryReserveError;
78
79#[cfg(not(test))]
80impl TryReserveError {
81    /// Details about the allocation that caused the error
82    #[inline]
83    #[must_use]
84    #[unstable(
85        feature = "try_reserve_kind",
86        reason = "Uncertain how much info should be exposed",
87        issue = "48043"
88    )]
89    #[rustc_const_unstable(feature = "const_heap", issue = "79597")]
90    pub const fn kind(&self) -> TryReserveErrorKind {
91        self.kind.clone()
92    }
93}
94
95/// Details of the allocation that caused a `TryReserveError`
96#[derive(#[automatically_derived]#[unstable(feature = "try_reserve_kind", reason =
"Uncertain how much info should be exposed", issue = "48043")]
impl ::core::marker::StructuralPartialEq for TryReserveErrorKind { }
#[automatically_derived]#[unstable(feature = "try_reserve_kind", reason =
"Uncertain how much info should be exposed", issue = "48043")]
impl ::core::cmp::PartialEq for TryReserveErrorKind {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        ::core::intrinsics::discriminant_value(self) ==
                ::core::intrinsics::discriminant_value(other) &&
            match (self, other) {
                (Self::AllocError { layout: __self_0, non_exhaustive: __self_1
                    }, Self::AllocError {
                    layout: __arg1_0, non_exhaustive: __arg1_1 }) =>
                    __self_1 == __arg1_1 && __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]#[unstable(feature = "try_reserve_kind", reason =
"Uncertain how much info should be exposed", issue = "48043")]
impl ::core::cmp::Eq for TryReserveErrorKind {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Layout>;
        let _: ::core::cmp::AssertParamIsEq<()>;
    }
}Eq, #[automatically_derived]#[unstable(feature = "try_reserve_kind", reason =
"Uncertain how much info should be exposed", issue = "48043")]
impl ::core::fmt::Debug for TryReserveErrorKind {
    #[inline]
    #[rustc_trivial_field_reads]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Self::CapacityOverflow =>
                ::core::fmt::Formatter::write_str(f, "CapacityOverflow"),
            Self::AllocError { layout: __self_0, non_exhaustive: __self_1 } =>
                ::core::fmt::Formatter::debug_struct_field2_finish(f,
                    "AllocError layout non_exhaustive", __self_0, &__self_1),
        }
    }
}Debug)]
97#[unstable(
98    feature = "try_reserve_kind",
99    reason = "Uncertain how much info should be exposed",
100    issue = "48043"
101)]
102#[cfg(not(test))]
103pub enum TryReserveErrorKind {
104    /// Error due to the computed capacity exceeding the collection's maximum
105    /// (usually `isize::MAX` bytes).
106    CapacityOverflow,
107
108    /// The memory allocator returned an error
109    AllocError {
110        /// The layout of allocation request that failed
111        layout: Layout,
112
113        #[doc(hidden)]
114        #[unstable(
115            feature = "container_error_extra",
116            issue = "none",
117            reason = "\
118            Enable exposing the allocator’s custom error value \
119            if an associated type is added in the future: \
120            https://github.com/rust-lang/wg-allocators/issues/23"
121        )]
122        non_exhaustive: (),
123    },
124}
125
126#[unstable(
127    feature = "try_reserve_kind",
128    reason = "Uncertain how much info should be exposed",
129    issue = "48043"
130)]
131#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
132#[cfg(not(test))]
133const impl Clone for TryReserveErrorKind {
134    fn clone(&self) -> Self {
135        match self {
136            TryReserveErrorKind::CapacityOverflow => TryReserveErrorKind::CapacityOverflow,
137            TryReserveErrorKind::AllocError { layout, non_exhaustive: () } => {
138                TryReserveErrorKind::AllocError { layout: *layout, non_exhaustive: () }
139            }
140        }
141    }
142}
143
144#[cfg(test)]
145pub use realalloc::collections::TryReserveErrorKind;
146
147#[unstable(
148    feature = "try_reserve_kind",
149    reason = "Uncertain how much info should be exposed",
150    issue = "48043"
151)]
152#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
153#[cfg(not(test))]
154const impl From<TryReserveErrorKind> for TryReserveError {
155    #[inline]
156    fn from(kind: TryReserveErrorKind) -> Self {
157        Self { kind }
158    }
159}
160
161#[unstable(feature = "try_reserve_kind", issue = "48043")]
162#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
163#[cfg(not(test))]
164const impl From<LayoutError> for TryReserveErrorKind {
165    /// Always evaluates to [`TryReserveErrorKind::CapacityOverflow`].
166    #[inline]
167    fn from(_: LayoutError) -> Self {
168        TryReserveErrorKind::CapacityOverflow
169    }
170}
171
172#[stable(feature = "try_reserve", since = "1.57.0")]
173#[cfg(not(test))]
174impl Display for TryReserveError {
175    fn fmt(
176        &self,
177        fmt: &mut core::fmt::Formatter<'_>,
178    ) -> core::result::Result<(), core::fmt::Error> {
179        fmt.write_str("memory allocation failed")?;
180        let reason = match self.kind {
181            TryReserveErrorKind::CapacityOverflow => {
182                " because the computed capacity exceeded the collection's maximum"
183            }
184            TryReserveErrorKind::AllocError { .. } => {
185                " because the memory allocator returned an error"
186            }
187        };
188        fmt.write_str(reason)
189    }
190}
191
192/// An intermediate trait for specialization of `Extend`.
193#[doc(hidden)]
194#[cfg(not(no_global_oom_handling))]
195trait SpecExtend<I: IntoIterator> {
196    /// Extends `self` with the contents of the given iterator.
197    fn spec_extend(&mut self, iter: I);
198}
199
200#[stable(feature = "try_reserve", since = "1.57.0")]
201#[cfg(not(test))]
202impl core::error::Error for TryReserveError {}