cl_aux/traits/
capacity_upper_bound.rs

1use crate::SingleItemStorage;
2#[cfg(feature = "alloc")]
3use alloc::{string::String, vec::Vec};
4
5/// See [`CapacityUpperBound::capacity_upper_bound`] for more information.
6pub trait CapacityUpperBound {
7  /// The maximum theoretical number of elements a type implementation is able to store.
8  const CAPACITY_UPPER_BOUND: usize;
9
10  /// Instance method representing [`Self::CAPACITY_UPPER_BOUND`].
11  #[inline]
12  fn capacity_upper_bound(&self) -> usize {
13    Self::CAPACITY_UPPER_BOUND
14  }
15}
16
17impl<T> CapacityUpperBound for &T
18where
19  T: CapacityUpperBound,
20{
21  const CAPACITY_UPPER_BOUND: usize = T::CAPACITY_UPPER_BOUND;
22
23  #[inline]
24  fn capacity_upper_bound(&self) -> usize {
25    (*self).capacity_upper_bound()
26  }
27}
28
29/// ```rust
30/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&()), 0);
31/// ```
32impl CapacityUpperBound for () {
33  const CAPACITY_UPPER_BOUND: usize = 0;
34}
35
36/// ```rust
37/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&Some(0)), 1);
38/// ```
39impl<T> CapacityUpperBound for Option<T> {
40  const CAPACITY_UPPER_BOUND: usize = 1;
41}
42
43/// ```rust
44/// let mut structure = cl_aux::doc_tests::single_item_storage();
45/// assert_eq!(cl_aux::Capacity::capacity(&structure), 1);
46/// ```
47impl<T> CapacityUpperBound for SingleItemStorage<T> {
48  const CAPACITY_UPPER_BOUND: usize = 1;
49}
50
51/// ```rust
52/// let mut structure = cl_aux::doc_tests::array();
53/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&structure), 3);
54/// ```
55impl<T, const N: usize> CapacityUpperBound for [T; N] {
56  const CAPACITY_UPPER_BOUND: usize = N;
57}
58
59/// ```rust
60/// let structure = cl_aux::doc_tests::slice();
61/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&structure), 2305843009213693951);
62/// ```
63impl<T> CapacityUpperBound for &'_ [T] {
64  const CAPACITY_UPPER_BOUND: usize = capacity_upper_bound_of_type::<T>();
65}
66
67/// ```rust
68/// let mut structure = cl_aux::doc_tests::slice_mut!();
69/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&mut structure), 2305843009213693951);
70/// ```
71impl<T> CapacityUpperBound for &'_ mut [T] {
72  const CAPACITY_UPPER_BOUND: usize = capacity_upper_bound_of_type::<T>();
73}
74
75/// ```rust
76/// let mut structure = cl_aux::doc_tests::string();
77/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&structure), 9223372036854775807);
78/// ```
79#[cfg(feature = "alloc")]
80impl CapacityUpperBound for String {
81  const CAPACITY_UPPER_BOUND: usize = capacity_upper_bound_of_type::<u8>();
82}
83
84/// ```rust
85/// let mut structure = cl_aux::doc_tests::vec();
86/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&structure), 2305843009213693951);
87/// ```
88#[cfg(feature = "alloc")]
89impl<T> CapacityUpperBound for Vec<T> {
90  const CAPACITY_UPPER_BOUND: usize = capacity_upper_bound_of_type::<T>();
91}
92
93/// ```rust
94/// let mut structure = cl_aux::doc_tests::array_string();
95/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&structure), 10);
96/// ```
97#[cfg(feature = "arrayvec")]
98impl<const N: usize> CapacityUpperBound for arrayvec::ArrayString<N> {
99  const CAPACITY_UPPER_BOUND: usize = N;
100}
101
102/// ```rust
103/// let mut structure = cl_aux::doc_tests::array_vec();
104/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&structure), 5);
105/// ```
106#[cfg(feature = "arrayvec")]
107impl<T, const N: usize> CapacityUpperBound for arrayvec::ArrayVec<T, N> {
108  const CAPACITY_UPPER_BOUND: usize = N;
109}
110
111/// ```rust
112/// let mut structure = cl_aux::doc_tests::small_vec();
113/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&structure), 2305843009213693951);
114/// ```
115#[cfg(feature = "smallvec")]
116impl<A> CapacityUpperBound for smallvec::SmallVec<A>
117where
118  A: smallvec::Array,
119{
120  const CAPACITY_UPPER_BOUND: usize = capacity_upper_bound_of_type::<A::Item>();
121}
122
123/// ```rust
124/// let mut structure = cl_aux::doc_tests::tiny_vec_array_vec();
125/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&structure), 5);
126/// ```
127#[cfg(feature = "tinyvec")]
128impl<A> CapacityUpperBound for tinyvec::ArrayVec<A>
129where
130  A: tinyvec::Array,
131  A::Item: Default,
132{
133  const CAPACITY_UPPER_BOUND: usize = A::CAPACITY;
134}
135
136/// ```rust
137/// let mut structure = cl_aux::doc_tests::tiny_vec_tiny_vec();
138/// assert_eq!(cl_aux::CapacityUpperBound::capacity_upper_bound(&structure), 2305843009213693951);
139/// ```
140#[cfg(all(feature = "alloc", feature = "tinyvec"))]
141impl<A> CapacityUpperBound for tinyvec::TinyVec<A>
142where
143  A: tinyvec::Array,
144  A::Item: Default,
145{
146  const CAPACITY_UPPER_BOUND: usize = capacity_upper_bound_of_type::<A::Item>();
147}
148
149#[inline]
150const fn capacity_upper_bound_of_type<T>() -> usize {
151  let isize_max_usize = isize::MAX.unsigned_abs();
152  if let Some(elem) = isize_max_usize.checked_div(size_of::<T>()) {
153    elem
154  } else {
155    0
156  }
157}