1use core::mem::MaybeUninit;
4#[cfg(feature = "alloc")]
5use std::boxed::Box;
6
7mod array;
8#[cfg(any(doc, feature = "alloc"))]
9pub(crate) mod heap;
10mod slice;
11
12mod capacity;
13
14pub struct AllocError;
16pub type AllocResult = Result<(), AllocError>;
18
19pub unsafe trait Storage: AsRef<[MaybeUninit<Self::Item>]> + AsMut<[MaybeUninit<Self::Item>]> {
25 type Item;
27
28 #[doc(hidden)]
29 const CONST_CAPACITY: Option<usize> = None;
30
31 fn reserve(&mut self, new_capacity: usize);
43
44 fn try_reserve(&mut self, new_capacity: usize) -> AllocResult;
52}
53
54pub unsafe trait StorageWithCapacity: Storage + Sized {
61 fn with_capacity(capacity: usize) -> Self;
63
64 #[doc(hidden)]
65 #[allow(non_snake_case)]
66 fn __with_capacity__const_capacity_checked(capacity: usize, _old_capacity: Option<usize>) -> Self {
67 Self::with_capacity(capacity)
68 }
69}
70
71unsafe impl<S: ?Sized + Storage> Storage for &mut S {
72 type Item = S::Item;
73
74 #[doc(hidden)]
75 const CONST_CAPACITY: Option<usize> = S::CONST_CAPACITY;
76
77 #[inline]
78 fn reserve(&mut self, new_capacity: usize) { S::reserve(self, new_capacity); }
79 #[inline]
80 fn try_reserve(&mut self, new_capacity: usize) -> AllocResult { S::try_reserve(self, new_capacity) }
81}
82
83#[cfg(any(doc, feature = "alloc"))]
85pub struct BoxStorage<S: ?Sized + Storage>(pub Box<S>);
86
87#[cfg(any(doc, feature = "alloc"))]
88impl<S: ?Sized + Storage> AsRef<[MaybeUninit<S::Item>]> for BoxStorage<S> {
89 fn as_ref(&self) -> &[MaybeUninit<S::Item>] { self.0.as_ref().as_ref() }
90}
91
92#[cfg(any(doc, feature = "alloc"))]
93impl<S: ?Sized + Storage> AsMut<[MaybeUninit<S::Item>]> for BoxStorage<S> {
94 fn as_mut(&mut self) -> &mut [MaybeUninit<S::Item>] { self.0.as_mut().as_mut() }
95}
96
97#[cfg(any(doc, feature = "alloc"))]
98unsafe impl<S: ?Sized + Storage> Storage for BoxStorage<S> {
99 type Item = S::Item;
100
101 #[doc(hidden)]
102 const CONST_CAPACITY: Option<usize> = S::CONST_CAPACITY;
103
104 #[inline]
105 fn reserve(&mut self, new_capacity: usize) { S::reserve(&mut self.0, new_capacity); }
106 #[inline]
107 fn try_reserve(&mut self, new_capacity: usize) -> AllocResult { S::try_reserve(&mut self.0, new_capacity) }
108}
109
110#[cfg(any(doc, feature = "alloc"))]
111unsafe impl<S: ?Sized + StorageWithCapacity> StorageWithCapacity for BoxStorage<S> {
112 fn with_capacity(capacity: usize) -> Self { Self(Box::new(S::with_capacity(capacity))) }
113
114 #[doc(hidden)]
115 #[allow(non_snake_case)]
116 fn __with_capacity__const_capacity_checked(capacity: usize, old_capacity: Option<usize>) -> Self {
117 Self(Box::new(S::__with_capacity__const_capacity_checked(
118 capacity,
119 old_capacity,
120 )))
121 }
122}