]> git.proxmox.com Git - rustc.git/blob - library/alloc/src/collections/mod.rs
New upstream version 1.55.0+dfsg1
[rustc.git] / library / alloc / src / collections / mod.rs
1 //! Collection types.
2
3 #![stable(feature = "rust1", since = "1.0.0")]
4
5 #[cfg(not(no_global_oom_handling))]
6 pub mod binary_heap;
7 #[cfg(not(no_global_oom_handling))]
8 mod btree;
9 #[cfg(not(no_global_oom_handling))]
10 pub mod linked_list;
11 #[cfg(not(no_global_oom_handling))]
12 pub mod vec_deque;
13
14 #[cfg(not(no_global_oom_handling))]
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
22 #[cfg(not(no_global_oom_handling))]
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
30 #[cfg(not(no_global_oom_handling))]
31 #[stable(feature = "rust1", since = "1.0.0")]
32 #[doc(no_inline)]
33 pub use binary_heap::BinaryHeap;
34
35 #[cfg(not(no_global_oom_handling))]
36 #[stable(feature = "rust1", since = "1.0.0")]
37 #[doc(no_inline)]
38 pub use btree_map::BTreeMap;
39
40 #[cfg(not(no_global_oom_handling))]
41 #[stable(feature = "rust1", since = "1.0.0")]
42 #[doc(no_inline)]
43 pub use btree_set::BTreeSet;
44
45 #[cfg(not(no_global_oom_handling))]
46 #[stable(feature = "rust1", since = "1.0.0")]
47 #[doc(no_inline)]
48 pub use linked_list::LinkedList;
49
50 #[cfg(not(no_global_oom_handling))]
51 #[stable(feature = "rust1", since = "1.0.0")]
52 #[doc(no_inline)]
53 pub use vec_deque::VecDeque;
54
55 use crate::alloc::{Layout, LayoutError};
56 use core::fmt::Display;
57
58 /// The error type for `try_reserve` methods.
59 #[derive(Clone, PartialEq, Eq, Debug)]
60 #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
61 pub enum TryReserveError {
62 /// Error due to the computed capacity exceeding the collection's maximum
63 /// (usually `isize::MAX` bytes).
64 CapacityOverflow,
65
66 /// The memory allocator returned an error
67 AllocError {
68 /// The layout of allocation request that failed
69 layout: Layout,
70
71 #[doc(hidden)]
72 #[unstable(
73 feature = "container_error_extra",
74 issue = "none",
75 reason = "\
76 Enable exposing the allocator’s custom error value \
77 if an associated type is added in the future: \
78 https://github.com/rust-lang/wg-allocators/issues/23"
79 )]
80 non_exhaustive: (),
81 },
82 }
83
84 #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
85 impl From<LayoutError> for TryReserveError {
86 /// Always evaluates to [`TryReserveError::CapacityOverflow`].
87 #[inline]
88 fn from(_: LayoutError) -> Self {
89 TryReserveError::CapacityOverflow
90 }
91 }
92
93 #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
94 impl Display for TryReserveError {
95 fn fmt(
96 &self,
97 fmt: &mut core::fmt::Formatter<'_>,
98 ) -> core::result::Result<(), core::fmt::Error> {
99 fmt.write_str("memory allocation failed")?;
100 let reason = match &self {
101 TryReserveError::CapacityOverflow => {
102 " because the computed capacity exceeded the collection's maximum"
103 }
104 TryReserveError::AllocError { .. } => " because the memory allocator returned a error",
105 };
106 fmt.write_str(reason)
107 }
108 }
109
110 /// An intermediate trait for specialization of `Extend`.
111 #[doc(hidden)]
112 trait SpecExtend<I: IntoIterator> {
113 /// Extends `self` with the contents of the given iterator.
114 fn spec_extend(&mut self, iter: I);
115 }