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