cl-aux 5.1.0

Provides elements that describe collections
Documentation
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// See [`Swap::swap`] for more information.
pub trait Swap {
  /// Error
  type Error;
  /// Input
  type Input;

  /// Swaps two elements referencied by `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)
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::array();
/// cl_aux::Swap::swap(&mut structure, [0, 2]);
/// assert_eq!(structure.get(0), Some(&3));
/// assert_eq!(structure.get(2), Some(&1));
/// ```
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)
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::slice_mut!();
/// cl_aux::Swap::swap(&mut structure, [0, 2]);
/// assert_eq!(structure.get(0), Some(&3));
/// assert_eq!(structure.get(2), Some(&1));
/// ```
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)
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::vec();
/// cl_aux::Swap::swap(&mut structure, [0, 2]);
/// assert_eq!(structure.get(0), Some(&3));
/// assert_eq!(structure.get(2), Some(&1));
/// ```
#[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)
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::array_vec();
/// cl_aux::Swap::swap(&mut structure, [0, 2]);
/// assert_eq!(structure.get(0), Some(&3));
/// assert_eq!(structure.get(2), Some(&1));
/// ```
#[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)
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::small_vec();
/// cl_aux::Swap::swap(&mut structure, [0, 2]);
/// assert_eq!(structure.get(0), Some(&3));
/// assert_eq!(structure.get(2), Some(&1));
/// ```
#[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)
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::tiny_vec_array_vec();
/// cl_aux::Swap::swap(&mut structure, [0, 2]);
/// assert_eq!(structure.get(0), Some(&3));
/// assert_eq!(structure.get(2), Some(&1));
/// ```
#[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)
  }
}

/// ```rust
/// let mut structure = cl_aux::doc_tests::tiny_vec_tiny_vec();
/// cl_aux::Swap::swap(&mut structure, [0, 2]);
/// assert_eq!(structure.get(0), Some(&3));
/// assert_eq!(structure.get(2), Some(&1));
/// ```
#[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(())
}