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
/// Safely retrieve items from a collection with negative indexing.
pub trait GetBack {
  type Item;

  /// Safely retrieve items from a collection with negative indexing.
  /// Returns `None` if the index is larger than the length of the collection.
  fn get_back(&self, index: usize) -> Option<Self::Item>;
}

impl<T: Copy> GetBack for Vec<T> {
  type Item = T;
  fn get_back(&self, index: usize) -> Option<Self::Item> {
      self.len()
          .checked_sub(index)
          .and_then(|index| self.get(index)).copied()
  }
}

/// Safely retrieve a mutable reference from a collection with negative indexing.
pub trait GetBackMut {
  type Item;

  /// Safely retrieve a mutable reference from a collection with negative indexing.
  /// Returns `None` if the index is larger than the length of the collection.
  fn get_back_mut(&mut self, index: usize) -> Option<&mut Self::Item>;
}

impl<T> GetBackMut for Vec<T> {
  type Item = T;
  fn get_back_mut(&mut self, index: usize) -> Option<&mut Self::Item> {
      self.len()
          .checked_sub(index)
          .and_then(|index| self.get_mut(index))
  }
}