1use crate::Matrix;
2
3mod private {
4
5 pub trait Sealed {}
6 impl Sealed for usize {}
7 impl Sealed for (usize, usize) {}
8}
9
10pub unsafe trait MatrixIndex<T: ?Sized>: private::Sealed {
21 type Output: ?Sized;
23
24 fn get(self, matrix: &T) -> Option<&Self::Output>;
27
28 fn get_mut(self, matrix: &mut T) -> Option<&mut Self::Output>;
31
32 unsafe fn get_unchecked(self, matrix: *const T) -> *const Self::Output;
43
44 unsafe fn get_unchecked_mut(self, stride: *mut T) -> *mut Self::Output;
55
56 #[track_caller]
59 fn index(self, stride: &T) -> &Self::Output;
60
61 #[track_caller]
64 fn index_mut(self, stride: &mut T) -> &mut Self::Output;
65}
66
67unsafe impl<T, const M: usize, const N: usize> MatrixIndex<Matrix<M, N, T>> for usize {
68 type Output = T;
69
70 #[inline]
71 fn get(self, matrix: &Matrix<M, N, T>) -> Option<&Self::Output> {
72 matrix.as_slice().get(self)
73 }
74
75 #[inline]
76 fn get_mut(self, matrix: &mut Matrix<M, N, T>) -> Option<&mut Self::Output> {
77 matrix.as_mut_slice().get_mut(self)
78 }
79
80 #[inline]
81 unsafe fn get_unchecked(self, matrix: *const Matrix<M, N, T>) -> *const Self::Output {
82 let matrix = unsafe { (*matrix).as_slice() };
85 unsafe { matrix.get_unchecked(self) }
86 }
87
88 #[inline]
89 unsafe fn get_unchecked_mut(self, matrix: *mut Matrix<M, N, T>) -> *mut Self::Output {
90 let matrix = unsafe { (*matrix).as_mut_slice() };
93 unsafe { matrix.get_unchecked_mut(self) }
94 }
95
96 #[track_caller]
97 #[inline]
98 fn index(self, matrix: &Matrix<M, N, T>) -> &Self::Output {
99 &matrix.as_slice()[self]
100 }
101
102 #[track_caller]
103 #[inline]
104 fn index_mut(self, matrix: &mut Matrix<M, N, T>) -> &mut Self::Output {
105 &mut matrix.as_mut_slice()[self]
106 }
107}
108
109unsafe impl<T, const M: usize, const N: usize> MatrixIndex<Matrix<M, N, T>> for (usize, usize) {
110 type Output = T;
111
112 #[inline]
113 fn get(self, matrix: &Matrix<M, N, T>) -> Option<&Self::Output> {
114 matrix.as_slice().get(self.1 * M + self.0)
115 }
116
117 #[inline]
118 fn get_mut(self, matrix: &mut Matrix<M, N, T>) -> Option<&mut Self::Output> {
119 matrix.as_mut_slice().get_mut(self.1 * M + self.0)
120 }
121
122 #[inline]
123 unsafe fn get_unchecked(self, matrix: *const Matrix<M, N, T>) -> *const Self::Output {
124 let matrix = unsafe { (*matrix).as_slice() };
127 unsafe { matrix.get_unchecked(self.1 * M + self.0) }
128 }
129
130 #[inline]
131 unsafe fn get_unchecked_mut(self, matrix: *mut Matrix<M, N, T>) -> *mut Self::Output {
132 let matrix = unsafe { (*matrix).as_mut_slice() };
135 unsafe { matrix.get_unchecked_mut(self.1 * M + self.0) }
136 }
137
138 #[track_caller]
139 #[inline]
140 fn index(self, matrix: &Matrix<M, N, T>) -> &Self::Output {
141 &matrix.as_slice()[self.1 * M + self.0]
142 }
143
144 #[track_caller]
145 #[inline]
146 fn index_mut(self, matrix: &mut Matrix<M, N, T>) -> &mut Self::Output {
147 &mut matrix.as_mut_slice()[self.1 * M + self.0]
148 }
149}