use crate::SingleItemStorage;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
pub trait GetMut {
type Error;
type Input;
type Output<'output>
where
Self: 'output;
fn get_mut(&mut self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error>;
}
impl<T> GetMut for Option<T> {
type Error = crate::Error;
type Input = usize;
type Output<'output> = &'output mut T
where
Self: 'output;
#[inline]
fn get_mut(&mut self, _: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
if let &mut Some(ref mut el) = self {
Ok(el)
} else {
Err(crate::Error::OutOfBounds(stringify!(self), 1))
}
}
}
impl<T> GetMut for SingleItemStorage<T> {
type Error = crate::Error;
type Input = usize;
type Output<'output> = &'output mut T
where
Self: 'output;
#[inline]
fn get_mut(&mut self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
_get_mut!(self.as_mut(), input)
}
}
impl<T, const N: usize> GetMut for [T; N] {
type Error = crate::Error;
type Input = usize;
type Output<'output> = &'output mut T
where
Self: 'output;
#[inline]
fn get_mut(&mut self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
_get_mut!(self.as_mut(), input)
}
}
impl<T> GetMut for &'_ mut [T] {
type Error = crate::Error;
type Input = usize;
type Output<'output> = &'output mut T
where
Self: 'output;
#[inline]
fn get_mut(&mut self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
_get_mut!(self.as_mut(), input)
}
}
#[cfg(feature = "alloc")]
impl<T> GetMut for Vec<T> {
type Error = crate::Error;
type Input = usize;
type Output<'output> = &'output mut T
where
Self: 'output;
#[inline]
fn get_mut(&mut self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
_get_mut!(self.as_mut_slice(), input)
}
}
#[cfg(feature = "arrayvec")]
impl<T, const N: usize> GetMut for arrayvec::ArrayVec<T, N> {
type Error = crate::Error;
type Input = usize;
type Output<'output> = &'output mut T
where
Self: 'output;
#[inline]
fn get_mut(&mut self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
_get_mut!(self.as_mut(), input)
}
}
#[cfg(feature = "smallvec")]
impl<A> GetMut for smallvec::SmallVec<A>
where
A: smallvec::Array,
{
type Error = crate::Error;
type Input = usize;
type Output<'output> = &'output mut A::Item
where
Self: 'output;
#[inline]
fn get_mut(&mut self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
_get_mut!(self.as_mut(), input)
}
}
#[cfg(feature = "tinyvec")]
impl<A> GetMut for tinyvec::ArrayVec<A>
where
A: tinyvec::Array,
{
type Error = crate::Error;
type Input = usize;
type Output<'output> = &'output mut A::Item
where
Self: 'output;
#[inline]
fn get_mut(&mut self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
_get_mut!(self.as_mut(), input)
}
}
#[cfg(feature = "tinyvec")]
impl<T, const N: usize> GetMut for staticvec::StaticVec<T, N> {
type Error = crate::Error;
type Input = usize;
type Output<'output> = &'output mut T
where
Self: 'output;
#[inline]
fn get_mut(&mut self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
_get_mut!(self.as_mut(), input)
}
}
#[cfg(feature = "tinyvec")]
impl<A> GetMut for tinyvec::TinyVec<A>
where
A: tinyvec::Array,
A::Item: Default,
{
type Error = crate::Error;
type Input = usize;
type Output<'output> = &'output mut A::Item
where
Self: 'output;
#[inline]
fn get_mut(&mut self, input: Self::Input) -> Result<Self::Output<'_>, Self::Error> {
_get_mut!(self.as_mut(), input)
}
}