cl_aux/traits/
swap.rs

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