]>
Commit | Line | Data |
---|---|---|
8faf50e0 XL |
1 | //! Collection types. |
2 | ||
3 | #![stable(feature = "rust1", since = "1.0.0")] | |
4 | ||
17df50a5 | 5 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 | 6 | pub mod binary_heap; |
17df50a5 | 7 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 | 8 | mod btree; |
17df50a5 | 9 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 | 10 | pub mod linked_list; |
17df50a5 | 11 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 XL |
12 | pub mod vec_deque; |
13 | ||
17df50a5 | 14 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 XL |
15 | #[stable(feature = "rust1", since = "1.0.0")] |
16 | pub mod btree_map { | |
17 | //! A map based on a B-Tree. | |
18 | #[stable(feature = "rust1", since = "1.0.0")] | |
19 | pub use super::btree::map::*; | |
20 | } | |
21 | ||
17df50a5 | 22 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 XL |
23 | #[stable(feature = "rust1", since = "1.0.0")] |
24 | pub mod btree_set { | |
25 | //! A set based on a B-Tree. | |
26 | #[stable(feature = "rust1", since = "1.0.0")] | |
27 | pub use super::btree::set::*; | |
28 | } | |
29 | ||
17df50a5 | 30 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 XL |
31 | #[stable(feature = "rust1", since = "1.0.0")] |
32 | #[doc(no_inline)] | |
9fa01778 | 33 | pub use binary_heap::BinaryHeap; |
8faf50e0 | 34 | |
17df50a5 | 35 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 XL |
36 | #[stable(feature = "rust1", since = "1.0.0")] |
37 | #[doc(no_inline)] | |
9fa01778 | 38 | pub use btree_map::BTreeMap; |
8faf50e0 | 39 | |
17df50a5 | 40 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 XL |
41 | #[stable(feature = "rust1", since = "1.0.0")] |
42 | #[doc(no_inline)] | |
9fa01778 | 43 | pub use btree_set::BTreeSet; |
8faf50e0 | 44 | |
17df50a5 | 45 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 XL |
46 | #[stable(feature = "rust1", since = "1.0.0")] |
47 | #[doc(no_inline)] | |
9fa01778 | 48 | pub use linked_list::LinkedList; |
8faf50e0 | 49 | |
17df50a5 | 50 | #[cfg(not(no_global_oom_handling))] |
8faf50e0 XL |
51 | #[stable(feature = "rust1", since = "1.0.0")] |
52 | #[doc(no_inline)] | |
9fa01778 | 53 | pub use vec_deque::VecDeque; |
8faf50e0 | 54 | |
fc512014 | 55 | use crate::alloc::{Layout, LayoutError}; |
ba9703b0 | 56 | use core::fmt::Display; |
8faf50e0 | 57 | |
e1599b0c | 58 | /// The error type for `try_reserve` methods. |
8faf50e0 | 59 | #[derive(Clone, PartialEq, Eq, Debug)] |
dfeec247 | 60 | #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")] |
94222f64 XL |
61 | pub struct TryReserveError { |
62 | kind: TryReserveErrorKind, | |
63 | } | |
64 | ||
65 | impl TryReserveError { | |
66 | /// Details about the allocation that caused the error | |
67 | #[inline] | |
68 | #[unstable( | |
69 | feature = "try_reserve_kind", | |
70 | reason = "Uncertain how much info should be exposed", | |
71 | issue = "48043" | |
72 | )] | |
73 | pub fn kind(&self) -> TryReserveErrorKind { | |
74 | self.kind.clone() | |
75 | } | |
76 | } | |
77 | ||
78 | /// Details of the allocation that caused a `TryReserveError` | |
79 | #[derive(Clone, PartialEq, Eq, Debug)] | |
80 | #[unstable( | |
81 | feature = "try_reserve_kind", | |
82 | reason = "Uncertain how much info should be exposed", | |
83 | issue = "48043" | |
84 | )] | |
85 | pub enum TryReserveErrorKind { | |
8faf50e0 XL |
86 | /// Error due to the computed capacity exceeding the collection's maximum |
87 | /// (usually `isize::MAX` bytes). | |
88 | CapacityOverflow, | |
8faf50e0 | 89 | |
e1599b0c XL |
90 | /// The memory allocator returned an error |
91 | AllocError { | |
92 | /// The layout of allocation request that failed | |
93 | layout: Layout, | |
94 | ||
95 | #[doc(hidden)] | |
dfeec247 XL |
96 | #[unstable( |
97 | feature = "container_error_extra", | |
98 | issue = "none", | |
99 | reason = "\ | |
e1599b0c XL |
100 | Enable exposing the allocator’s custom error value \ |
101 | if an associated type is added in the future: \ | |
dfeec247 XL |
102 | https://github.com/rust-lang/wg-allocators/issues/23" |
103 | )] | |
e1599b0c XL |
104 | non_exhaustive: (), |
105 | }, | |
8faf50e0 XL |
106 | } |
107 | ||
94222f64 XL |
108 | #[unstable( |
109 | feature = "try_reserve_kind", | |
110 | reason = "Uncertain how much info should be exposed", | |
111 | issue = "48043" | |
112 | )] | |
113 | impl From<TryReserveErrorKind> for TryReserveError { | |
114 | #[inline] | |
115 | fn from(kind: TryReserveErrorKind) -> Self { | |
116 | Self { kind } | |
117 | } | |
118 | } | |
119 | ||
120 | #[unstable(feature = "try_reserve_kind", reason = "new API", issue = "48043")] | |
121 | impl From<LayoutError> for TryReserveErrorKind { | |
122 | /// Always evaluates to [`TryReserveErrorKind::CapacityOverflow`]. | |
8faf50e0 | 123 | #[inline] |
fc512014 | 124 | fn from(_: LayoutError) -> Self { |
94222f64 | 125 | TryReserveErrorKind::CapacityOverflow |
8faf50e0 XL |
126 | } |
127 | } | |
128 | ||
ba9703b0 XL |
129 | #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")] |
130 | impl Display for TryReserveError { | |
131 | fn fmt( | |
132 | &self, | |
133 | fmt: &mut core::fmt::Formatter<'_>, | |
134 | ) -> core::result::Result<(), core::fmt::Error> { | |
135 | fmt.write_str("memory allocation failed")?; | |
94222f64 XL |
136 | let reason = match self.kind { |
137 | TryReserveErrorKind::CapacityOverflow => { | |
ba9703b0 XL |
138 | " because the computed capacity exceeded the collection's maximum" |
139 | } | |
94222f64 XL |
140 | TryReserveErrorKind::AllocError { .. } => { |
141 | " because the memory allocator returned a error" | |
142 | } | |
ba9703b0 XL |
143 | }; |
144 | fmt.write_str(reason) | |
145 | } | |
146 | } | |
147 | ||
8faf50e0 XL |
148 | /// An intermediate trait for specialization of `Extend`. |
149 | #[doc(hidden)] | |
150 | trait SpecExtend<I: IntoIterator> { | |
151 | /// Extends `self` with the contents of the given iterator. | |
152 | fn spec_extend(&mut self, iter: I); | |
153 | } |