cl_aux/traits/
get_mut.rs

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