cl_aux/traits/
capacity.rs

1use crate::SingleItemStorage;
2#[cfg(feature = "alloc")]
3use alloc::{string::String, vec::Vec};
4
5/// See [`Capacity::capacity`] for more information.
6pub trait Capacity {
7  /// The number of elements the implementation has pre-allocated as an internal buffer. Not
8  /// necessarily the current number of inserted elements.
9  fn capacity(&self) -> usize;
10}
11
12impl<T> Capacity for &T
13where
14  T: Capacity,
15{
16  #[inline]
17  fn capacity(&self) -> usize {
18    (*self).capacity()
19  }
20}
21
22/// ```rust
23/// assert_eq!(cl_aux::Capacity::capacity(&()), 0);
24/// ```
25impl Capacity for () {
26  #[inline]
27  fn capacity(&self) -> usize {
28    0
29  }
30}
31
32/// ```rust
33/// assert_eq!(cl_aux::Capacity::capacity(&Some(0)), 1);
34/// ```
35impl<T> Capacity for Option<T> {
36  #[inline]
37  fn capacity(&self) -> usize {
38    1
39  }
40}
41
42/// ```rust
43/// let mut structure = cl_aux::doc_tests::single_item_storage();
44/// assert_eq!(cl_aux::Capacity::capacity(&structure), 1);
45/// ```
46impl<T> Capacity for SingleItemStorage<T> {
47  #[inline]
48  fn capacity(&self) -> usize {
49    1
50  }
51}
52
53/// ```rust
54/// let mut structure = cl_aux::doc_tests::array();
55/// assert_eq!(cl_aux::Capacity::capacity(&structure), 3);
56/// ```
57impl<T, const N: usize> Capacity for [T; N] {
58  #[inline]
59  fn capacity(&self) -> usize {
60    N
61  }
62}
63
64/// ```rust
65/// let structure = cl_aux::doc_tests::slice();
66/// assert_eq!(cl_aux::Length::length(&structure), 3);
67/// ```
68impl<T> Capacity for &'_ [T] {
69  #[inline]
70  fn capacity(&self) -> usize {
71    self.len()
72  }
73}
74
75/// ```rust
76/// let mut structure = cl_aux::doc_tests::slice_mut!();
77/// assert_eq!(cl_aux::Length::length(&mut structure), 3);
78/// ```
79impl<T> Capacity for &'_ mut [T] {
80  #[inline]
81  fn capacity(&self) -> usize {
82    self.len()
83  }
84}
85
86/// ```rust
87/// let mut structure = cl_aux::doc_tests::string();
88/// assert_eq!(cl_aux::Capacity::capacity(&structure), 5);
89/// ```
90#[cfg(feature = "alloc")]
91impl Capacity for String {
92  #[inline]
93  fn capacity(&self) -> usize {
94    self.capacity()
95  }
96}
97
98/// ```rust
99/// let mut structure = cl_aux::doc_tests::vec();
100/// assert_eq!(cl_aux::Capacity::capacity(&structure), 5);
101/// ```
102#[cfg(feature = "alloc")]
103impl<T> Capacity for Vec<T> {
104  #[inline]
105  fn capacity(&self) -> usize {
106    self.capacity()
107  }
108}
109
110/// ```rust
111/// let mut structure = cl_aux::doc_tests::array_string();
112/// assert_eq!(cl_aux::Capacity::capacity(&structure), 10);
113/// ```
114#[cfg(feature = "arrayvec")]
115impl<const N: usize> Capacity for arrayvec::ArrayString<N> {
116  #[inline]
117  fn capacity(&self) -> usize {
118    self.capacity()
119  }
120}
121
122/// ```rust
123/// let mut structure = cl_aux::doc_tests::array_vec();
124/// assert_eq!(cl_aux::Capacity::capacity(&structure), 5);
125/// ```
126#[cfg(feature = "arrayvec")]
127impl<T, const N: usize> Capacity for arrayvec::ArrayVec<T, N> {
128  #[inline]
129  fn capacity(&self) -> usize {
130    self.capacity()
131  }
132}
133
134/// ```rust
135/// let mut structure = cl_aux::doc_tests::small_vec();
136/// assert_eq!(cl_aux::Capacity::capacity(&structure), 5);
137/// ```
138#[cfg(feature = "smallvec")]
139impl<A> Capacity for smallvec::SmallVec<A>
140where
141  A: smallvec::Array,
142{
143  #[inline]
144  fn capacity(&self) -> usize {
145    self.capacity()
146  }
147}
148
149/// ```rust
150/// let mut structure = cl_aux::doc_tests::tiny_vec_array_vec();
151/// assert_eq!(cl_aux::Capacity::capacity(&structure), 5);
152/// ```
153#[cfg(feature = "tinyvec")]
154impl<A> Capacity for tinyvec::ArrayVec<A>
155where
156  A: tinyvec::Array,
157  A::Item: Default,
158{
159  #[inline]
160  fn capacity(&self) -> usize {
161    self.capacity()
162  }
163}
164
165/// ```rust
166/// let mut structure = cl_aux::doc_tests::tiny_vec_tiny_vec();
167/// assert_eq!(cl_aux::Capacity::capacity(&structure), 5);
168/// ```
169#[cfg(all(feature = "alloc", feature = "tinyvec"))]
170impl<A> Capacity for tinyvec::TinyVec<A>
171where
172  A: tinyvec::Array,
173  A::Item: Default,
174{
175  #[inline]
176  fn capacity(&self) -> usize {
177    self.capacity()
178  }
179}