1#[cfg(feature = "alloc")]
2use alloc::vec::Vec;
3
4pub trait Swap {
6 type Error;
8 type Input;
10
11 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
28impl<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
44impl<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#[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#[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#[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#[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#[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}