cl_aux/traits/
with_capacity.rs1#[cfg(feature = "alloc")]
2use alloc::{string::String, vec::Vec};
3
4pub trait WithCapacity
6where
7 Self: Sized,
8{
9 type Error;
11 type Input;
13
14 fn with_capacity(input: Self::Input) -> Result<Self, Self::Error>;
16}
17
18impl<T, const N: usize> WithCapacity for [T; N]
25where
26 T: Default,
27{
28 type Error = crate::Error;
29 type Input = usize;
30
31 #[inline]
32 fn with_capacity(_: Self::Input) -> Result<Self, Self::Error> {
33 Ok([(); N].map(|_| T::default()))
34 }
35}
36
37#[cfg(feature = "alloc")]
42impl WithCapacity for String {
43 type Error = crate::Error;
44 type Input = usize;
45
46 #[inline]
47 fn with_capacity(input: Self::Input) -> Result<Self, Self::Error> {
48 Ok(String::with_capacity(input))
49 }
50}
51
52#[cfg(feature = "alloc")]
57impl<T> WithCapacity for Vec<T> {
58 type Error = crate::Error;
59 type Input = usize;
60
61 #[inline]
62 fn with_capacity(input: Self::Input) -> Result<Self, Self::Error> {
63 Ok(Vec::with_capacity(input))
64 }
65}
66
67#[cfg(feature = "arrayvec")]
73impl<const N: usize> WithCapacity for arrayvec::ArrayString<N> {
74 type Error = crate::Error;
75 type Input = usize;
76
77 #[inline]
78 fn with_capacity(_: Self::Input) -> Result<Self, Self::Error> {
79 Ok(arrayvec::ArrayString::new())
80 }
81}
82
83#[cfg(feature = "arrayvec")]
89impl<T, const N: usize> WithCapacity for arrayvec::ArrayVec<T, N> {
90 type Error = crate::Error;
91 type Input = usize;
92
93 #[inline]
94 fn with_capacity(_: Self::Input) -> Result<Self, Self::Error> {
95 Ok(arrayvec::ArrayVec::new())
96 }
97}
98
99#[cfg(feature = "smallvec")]
105impl<A> WithCapacity for smallvec::SmallVec<A>
106where
107 A: smallvec::Array,
108{
109 type Error = crate::Error;
110 type Input = usize;
111
112 #[inline]
113 fn with_capacity(input: Self::Input) -> Result<Self, Self::Error> {
114 Ok(smallvec::SmallVec::with_capacity(input))
115 }
116}
117
118#[cfg(feature = "tinyvec")]
124impl<A> WithCapacity for tinyvec::ArrayVec<A>
125where
126 A: Default + tinyvec::Array,
127 A::Item: Default,
128{
129 type Error = crate::Error;
130 type Input = usize;
131
132 #[inline]
133 fn with_capacity(_: Self::Input) -> Result<Self, Self::Error> {
134 Ok(tinyvec::ArrayVec::new())
135 }
136}
137
138#[cfg(all(feature = "alloc", feature = "tinyvec"))]
144impl<A> WithCapacity for tinyvec::TinyVec<A>
145where
146 A: Default + tinyvec::Array,
147 A::Item: Default,
148{
149 type Error = crate::Error;
150 type Input = usize;
151
152 #[inline]
153 fn with_capacity(input: Self::Input) -> Result<Self, Self::Error> {
154 Ok(tinyvec::TinyVec::with_capacity(input))
155 }
156}