use crate::{Array, ArrayWrapper};
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::vec::Vec;
pub trait Swap {
type Input;
type Output;
fn swap(&mut self, input: Self::Input) -> Self::Output;
}
impl<A> Swap for ArrayWrapper<A>
where
A: Array,
{
type Input = [usize; 2];
type Output = ();
#[inline]
fn swap(&mut self, [a, b]: Self::Input) {
self.array.slice_mut().swap(a, b)
}
}
impl<'a, T> Swap for &'a mut [T] {
type Input = [usize; 2];
type Output = ();
#[inline]
fn swap(&mut self, [a, b]: Self::Input) {
self.as_mut().swap(a, b)
}
}
#[cfg(feature = "alloc")]
impl<T> Swap for Vec<T> {
type Input = [usize; 2];
type Output = ();
#[inline]
fn swap(&mut self, [a, b]: Self::Input) {
self.as_mut_slice().swap(a, b)
}
}
#[cfg(feature = "with-arrayvec")]
impl<A> Swap for arrayvec::ArrayVec<A>
where
A: arrayvec::Array,
{
type Input = [usize; 2];
type Output = ();
#[inline]
fn swap(&mut self, [a, b]: Self::Input) {
self.as_mut_slice().swap(a, b)
}
}
#[cfg(feature = "with-smallvec")]
impl<A> Swap for smallvec::SmallVec<A>
where
A: smallvec::Array,
{
type Input = [usize; 2];
type Output = ();
#[inline]
fn swap(&mut self, [a, b]: Self::Input) {
self.as_mut_slice().swap(a, b)
}
}
#[cfg(feature = "with-staticvec")]
impl<T, const N: usize> Swap for staticvec::StaticVec<T, N> {
type Input = [usize; 2];
type Output = ();
#[inline]
fn swap(&mut self, [a, b]: Self::Input) {
self.as_mut_slice().swap(a, b)
}
}
#[cfg(feature = "with-tinyvec")]
impl<A> Swap for tinyvec::ArrayVec<A>
where
A: tinyvec::Array,
A::Item: Default,
{
type Input = [usize; 2];
type Output = ();
#[inline]
fn swap(&mut self, [a, b]: Self::Input) {
self.as_mut_slice().swap(a, b)
}
}
#[cfg(all(feature = "alloc", feature = "with-tinyvec"))]
impl<A> Swap for tinyvec::TinyVec<A>
where
A: tinyvec::Array,
A::Item: Default,
{
type Input = [usize; 2];
type Output = ();
#[inline]
fn swap(&mut self, [a, b]: Self::Input) {
self.as_mut_slice().swap(a, b)
}
}