cl_aux/traits/
with_capacity.rs

1#[cfg(feature = "alloc")]
2use alloc::{string::String, vec::Vec};
3
4/// See [`WithCapacity::with_capacity`] for more information.
5pub trait WithCapacity
6where
7  Self: Sized,
8{
9  /// Error
10  type Error;
11  /// Input
12  type Input;
13
14  /// Creates a new instance based on an initial holding capacity provided by `Input`.
15  fn with_capacity(input: Self::Input) -> Result<Self, Self::Error>;
16}
17
18/// ```rust
19/// use cl_aux::Capacity;
20/// let structure: [i32; 5];
21/// structure = cl_aux::WithCapacity::with_capacity(0).unwrap();
22/// assert_eq!(structure.capacity(), 5);
23/// ```
24impl<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/// ```rust
38/// let structure: String = cl_aux::WithCapacity::with_capacity(2).unwrap();
39/// assert_eq!(structure.capacity(), 2);
40/// ```
41#[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/// ```rust
53/// let structure: Vec<i32> = cl_aux::WithCapacity::with_capacity(2).unwrap();
54/// assert_eq!(structure.capacity(), 2);
55/// ```
56#[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/// ```rust
68/// let structure: arrayvec::ArrayString<5>;
69/// structure = cl_aux::WithCapacity::with_capacity(0).unwrap();
70/// assert_eq!(structure.capacity(), 5);
71/// ```
72#[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/// ```rust
84/// let structure: arrayvec::ArrayVec<i32, 5>;
85/// structure = cl_aux::WithCapacity::with_capacity(0).unwrap();
86/// assert_eq!(structure.capacity(), 5);
87/// ```
88#[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/// ```rust
100/// let structure: smallvec::SmallVec<[i32; 5]>;
101/// structure = cl_aux::WithCapacity::with_capacity(0).unwrap();
102/// assert_eq!(structure.capacity(), 5);
103/// ```
104#[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/// ```rust
119/// let mut structure = cl_aux::doc_tests::tiny_vec_array_vec();
120/// structure = cl_aux::WithCapacity::with_capacity(0).unwrap();
121/// assert_eq!(structure.capacity(), 5);
122/// ```
123#[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/// ```rust
139/// let mut structure = cl_aux::doc_tests::tiny_vec_tiny_vec();
140/// structure = cl_aux::WithCapacity::with_capacity(0).unwrap();
141/// assert_eq!(structure.capacity(), 5);
142/// ```
143#[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}