#[cfg(feature = "alloc")]
use alloc::vec::Vec;
pub trait Swap {
type Error;
type Input;
fn swap(&mut self, input: Self::Input) -> Result<(), Self::Error>;
}
impl<T> Swap for &mut T
where
T: Swap,
{
type Error = T::Error;
type Input = T::Input;
#[inline]
fn swap(&mut self, input: Self::Input) -> Result<(), Self::Error> {
(*self).swap(input)
}
}
impl<T, const N: usize> Swap for [T; N] {
type Error = crate::Error;
type Input = [usize; 2];
#[inline]
fn swap(&mut self, [a, b]: Self::Input) -> Result<(), Self::Error> {
manage_slice(self, a, b)
}
}
impl<T> Swap for &'_ mut [T] {
type Error = crate::Error;
type Input = [usize; 2];
#[inline]
fn swap(&mut self, [a, b]: Self::Input) -> Result<(), Self::Error> {
manage_slice(self, a, b)
}
}
#[cfg(feature = "alloc")]
impl<T> Swap for Vec<T> {
type Error = crate::Error;
type Input = [usize; 2];
#[inline]
fn swap(&mut self, [a, b]: Self::Input) -> Result<(), Self::Error> {
manage_slice(self, a, b)
}
}
#[cfg(feature = "arrayvec")]
impl<T, const N: usize> Swap for arrayvec::ArrayVec<T, N> {
type Error = crate::Error;
type Input = [usize; 2];
#[inline]
fn swap(&mut self, [a, b]: Self::Input) -> Result<(), Self::Error> {
manage_slice(self, a, b)
}
}
#[cfg(feature = "smallvec")]
impl<A> Swap for smallvec::SmallVec<A>
where
A: smallvec::Array,
{
type Error = crate::Error;
type Input = [usize; 2];
#[inline]
fn swap(&mut self, [a, b]: Self::Input) -> Result<(), Self::Error> {
manage_slice(self, a, b)
}
}
#[cfg(feature = "tinyvec")]
impl<A> Swap for tinyvec::ArrayVec<A>
where
A: tinyvec::Array,
A::Item: Default,
{
type Error = crate::Error;
type Input = [usize; 2];
#[inline]
fn swap(&mut self, [a, b]: Self::Input) -> Result<(), Self::Error> {
manage_slice(self, a, b)
}
}
#[cfg(all(feature = "alloc", feature = "tinyvec"))]
impl<A> Swap for tinyvec::TinyVec<A>
where
A: tinyvec::Array,
A::Item: Default,
{
type Error = crate::Error;
type Input = [usize; 2];
#[inline]
fn swap(&mut self, [a, b]: Self::Input) -> Result<(), Self::Error> {
manage_slice(self, a, b)
}
}
fn manage_slice<T>(slice: &mut [T], a: usize, b: usize) -> crate::Result<()> {
_check_indcs!(&slice, a, b);
slice.as_mut().swap(a, b);
Ok(())
}