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    #[ferrocene::prevalidated]
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(PartialEq, Eq, 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#[ferrocene::prevalidated]
127#[unstable(
128    feature = "try_reserve_kind",
129    reason = "Uncertain how much info should be exposed",
130    issue = "48043"
131)]
132#[rustc_const_unstable(feature = "const_heap", issue = "79597")]
133#[cfg(not(test))]
134const impl Clone for TryReserveErrorKind {
135    fn clone(&self) -> Self {
136        match self {
137            TryReserveErrorKind::CapacityOverflow => TryReserveErrorKind::CapacityOverflow,
138            TryReserveErrorKind::AllocError { layout, non_exhaustive: () } => {
139                TryReserveErrorKind::AllocError { layout: *layout, non_exhaustive: () }
140            }
141        }
142    }
143}
144
145#[cfg(test)]
146pub use realalloc::collections::TryReserveErrorKind;
147
148#[ferrocene::prevalidated]
149#[unstable(
150    feature = "try_reserve_kind",
151    reason = "Uncertain how much info should be exposed",
152    issue = "48043"
153)]
154#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
155#[cfg(not(test))]
156const impl From<TryReserveErrorKind> for TryReserveError {
157    #[inline]
158    fn from(kind: TryReserveErrorKind) -> Self {
159        Self { kind }
160    }
161}
162
163#[unstable(feature = "try_reserve_kind", issue = "48043")]
164#[rustc_const_unstable(feature = "const_convert", issue = "143773")]
165#[cfg(not(test))]
166const impl From<LayoutError> for TryReserveErrorKind {
167    /// Always evaluates to [`TryReserveErrorKind::CapacityOverflow`].
168    #[inline]
169    fn from(_: LayoutError) -> Self {
170        TryReserveErrorKind::CapacityOverflow
171    }
172}
173
174#[stable(feature = "try_reserve", since = "1.57.0")]
175#[cfg(not(test))]
176impl Display for TryReserveError {
177    fn fmt(
178        &self,
179        fmt: &mut core::fmt::Formatter<'_>,
180    ) -> core::result::Result<(), core::fmt::Error> {
181        fmt.write_str("memory allocation failed")?;
182        let reason = match self.kind {
183            TryReserveErrorKind::CapacityOverflow => {
184                " because the computed capacity exceeded the collection's maximum"
185            }
186            TryReserveErrorKind::AllocError { .. } => {
187                " because the memory allocator returned an error"
188            }
189        };
190        fmt.write_str(reason)
191    }
192}
193
194/// An intermediate trait for specialization of `Extend`.
195#[doc(hidden)]
196#[cfg(not(no_global_oom_handling))]
197trait SpecExtend<I: IntoIterator> {
198    /// Extends `self` with the contents of the given iterator.
199    fn spec_extend(&mut self, iter: I);
200}
201
202#[stable(feature = "try_reserve", since = "1.57.0")]
203#[cfg(not(test))]
204impl core::error::Error for TryReserveError {}