1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
#[cfg(feature = "smallvec")]
use smallvec::{Array as SmallVecArray, SmallVec};

/// Has some way to extract a mutable reference from some sort of storage.
pub trait GetMut<T> {
  /// Input type for the [`get_mut`](GetMut::get_mut)` method.
  type GetMutInput;

  /// Extracts a mutable reference.
  fn get_mut(&mut self, input: Self::GetMutInput) -> Option<&mut T>;
}

#[cfg(feature = "smallvec")]
impl<A> GetMut<A::Item> for SmallVec<A>
where
  A: SmallVecArray,
{
  type GetMutInput = usize;

  fn get_mut(&mut self, input: Self::GetMutInput) -> Option<&mut A::Item> {
    self.as_mut_slice().get_mut(input)
  }
}

#[cfg(feature = "alloc")]
impl<T> GetMut<T> for Vec<T> {
  type GetMutInput = usize;

  fn get_mut(&mut self, input: Self::GetMutInput) -> Option<&mut T> {
    self.as_mut_slice().get_mut(input)
  }
}

impl<'a, T> GetMut<T> for &'a mut [T] {
  type GetMutInput = usize;

  fn get_mut(&mut self, input: Self::GetMutInput) -> Option<&mut T> {
    self.as_mut().get_mut(input)
  }
}

impl<T, const N: usize> GetMut<T> for [T; N] {
  type GetMutInput = usize;

  fn get_mut(&mut self, input: Self::GetMutInput) -> Option<&mut T>
  {
    self.as_mut().get_mut(input)
  }
}