cl_aux/traits/
get.rs

1use crate::SingleItemStorage;
2#[cfg(feature = "alloc")]
3use alloc::vec::Vec;
4
5/// See [`Get::get`] for more information.
6pub trait Get {
7  /// Error
8  type Error;
9  /// Input
10  type Input;
11  /// Output
12  type Output<'output>
13  where
14    Self: 'output;
15
16  /// Returns an immutable inner reference of a derived element.
17  fn get(&self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error>;
18}
19
20impl<T> Get for &T
21where
22  T: Get,
23{
24  type Error = T::Error;
25  type Input = T::Input;
26  type Output<'output>
27    = T::Output<'output>
28  where
29    Self: 'output;
30
31  #[inline]
32  fn get(&self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
33    (*self).get(input)
34  }
35}
36
37/// ```rust
38/// let structure = cl_aux::doc_tests::single_item_storage();
39/// assert_eq!(cl_aux::Get::get(&structure, 0), Ok(&1));
40/// ```
41impl<T> Get for SingleItemStorage<T> {
42  type Error = crate::Error;
43  type Input = usize;
44  type Output<'output>
45    = &'output T
46  where
47    Self: 'output;
48
49  #[inline]
50  fn get(&self, _: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
51    _get!(self.as_ref(), 0)
52  }
53}
54
55/// ```rust
56/// let structure = cl_aux::doc_tests::array();
57/// assert_eq!(cl_aux::Get::get(&structure, 0), Ok(&1));
58/// ```
59impl<T, const N: usize> Get for [T; N] {
60  type Error = crate::Error;
61  type Input = usize;
62  type Output<'output>
63    = &'output T
64  where
65    Self: 'output;
66
67  #[inline]
68  fn get(&self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
69    _get!(self.as_ref(), input)
70  }
71}
72
73/// ```rust
74/// let structure = cl_aux::doc_tests::slice();
75/// assert_eq!(cl_aux::Get::get(&structure, 0), Ok(&1));
76/// ```
77impl<T> Get for &'_ [T] {
78  type Error = crate::Error;
79  type Input = usize;
80  type Output<'output>
81    = &'output T
82  where
83    Self: 'output;
84
85  #[inline]
86  fn get(&self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
87    _get!(self.as_ref(), input)
88  }
89}
90
91/// ```rust
92/// let mut structure = cl_aux::doc_tests::slice_mut!();
93/// assert_eq!(cl_aux::Get::get(&mut structure, 0), Ok(&1));
94/// ```
95impl<T> Get for &'_ mut [T] {
96  type Error = crate::Error;
97  type Input = usize;
98  type Output<'output>
99    = &'output T
100  where
101    Self: 'output;
102
103  #[inline]
104  fn get(&self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
105    _get!(self.as_ref(), input)
106  }
107}
108
109/// ```rust
110/// let structure = cl_aux::doc_tests::vec();
111/// assert_eq!(cl_aux::Get::get(&structure, 0), Ok(&1));
112/// ```
113#[cfg(feature = "alloc")]
114impl<T> Get for Vec<T> {
115  type Error = crate::Error;
116  type Input = usize;
117  type Output<'output>
118    = &'output T
119  where
120    Self: 'output;
121
122  #[inline]
123  fn get(&self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
124    _get!(self.as_slice(), input)
125  }
126}
127
128/// ```rust
129/// let structure = cl_aux::doc_tests::array_vec();
130/// assert_eq!(cl_aux::Get::get(&structure, 0), Ok(&1));
131/// ```
132#[cfg(feature = "arrayvec")]
133impl<T, const N: usize> Get for arrayvec::ArrayVec<T, N> {
134  type Error = crate::Error;
135  type Input = usize;
136  type Output<'output>
137    = &'output T
138  where
139    Self: 'output;
140
141  #[inline]
142  fn get(&self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
143    _get!(self.as_ref(), input)
144  }
145}
146
147/// ```rust
148/// let structure = cl_aux::doc_tests::small_vec();
149/// assert_eq!(cl_aux::Get::get(&structure, 0), Ok(&1));
150/// ```
151#[cfg(feature = "smallvec")]
152impl<A> Get for smallvec::SmallVec<A>
153where
154  A: smallvec::Array,
155{
156  type Error = crate::Error;
157  type Input = usize;
158  type Output<'output>
159    = &'output A::Item
160  where
161    Self: 'output;
162
163  #[inline]
164  fn get(&self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
165    _get!(self.as_ref(), input)
166  }
167}
168
169/// ```rust
170/// let structure = cl_aux::doc_tests::tiny_vec_array_vec();
171/// assert_eq!(cl_aux::Get::get(&structure, 0), Ok(&1));
172/// ```
173#[cfg(feature = "tinyvec")]
174impl<A> Get for tinyvec::ArrayVec<A>
175where
176  A: tinyvec::Array,
177  A::Item: Default,
178{
179  type Error = crate::Error;
180  type Input = usize;
181  type Output<'output>
182    = &'output A::Item
183  where
184    Self: 'output;
185
186  #[inline]
187  fn get(&self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
188    _get!(self.as_ref(), input)
189  }
190}
191
192/// ```rust
193/// let structure = cl_aux::doc_tests::tiny_vec_tiny_vec();
194/// assert_eq!(cl_aux::Get::get(&structure, 0), Ok(&1));
195/// ```
196#[cfg(all(feature = "alloc", feature = "tinyvec"))]
197impl<A> Get for tinyvec::TinyVec<A>
198where
199  A: tinyvec::Array,
200  A::Item: Default,
201{
202  type Error = crate::Error;
203  type Input = usize;
204  type Output<'output>
205    = &'output A::Item
206  where
207    Self: 'output;
208
209  #[inline]
210  fn get(&self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
211    _get!(self.as_ref(), input)
212  }
213}