gat_std/
ops.rs

1//! GAT equivalents of `std` operators
2
3/// Index operator for immutable contexts. As opposed to `std`, the returned value can be a
4/// non-reference. This allows custom reference types for things like multi-dimensional matrices.
5pub trait Index<T> {
6    /// The output type of indexing this value
7    type Output<'a>
8    where
9        Self: 'a;
10
11    /// Get the value at this index immutably
12    fn index(&self, idx: T) -> Self::Output<'_>;
13}
14
15/// Index operator for mutable contexts. As opposed to `std`, the returned value can be a
16/// non-reference. This allows custom reference types for things like multi-dimensional matrices.
17pub trait IndexMut<T>: Index<T> {
18    /// The output type of indexing this value
19    type OutputMut<'a>
20    where
21        Self: 'a;
22
23    /// Get the value at this index mutably
24    fn index_mut(&mut self, idx: T) -> Self::OutputMut<'_>;
25}
26
27impl<T: ?Sized, I> Index<I> for T
28where
29    T: core::ops::Index<I>,
30    T::Output: 'static,
31{
32    type Output<'a> = &'a <T as core::ops::Index<I>>::Output
33    where
34        Self: 'a;
35
36    fn index(&self, idx: I) -> Self::Output<'_> {
37        <Self as core::ops::Index<I>>::index(self, idx)
38    }
39}
40
41impl<T: ?Sized, I> IndexMut<I> for T
42where
43    T: core::ops::IndexMut<I>,
44    T::Output: 'static,
45{
46    type OutputMut<'a> = &'a mut <T as core::ops::Index<I>>::Output
47    where
48        Self: 'a;
49
50    fn index_mut(&mut self, idx: I) -> Self::OutputMut<'_> {
51        <Self as core::ops::IndexMut<I>>::index_mut(self, idx)
52    }
53}