cl_aux/traits/
truncate.rs

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