|
1 | 1 | #[cfg(all(feature = "alloc", not(feature = "std")))]
|
2 | 2 | use alloc::vec::Vec;
|
3 | 3 | use simba::scalar::{SubsetOf, SupersetOf};
|
| 4 | +use std::borrow::{Borrow, BorrowMut}; |
4 | 5 | use std::convert::{AsMut, AsRef, From, Into};
|
5 | 6 |
|
6 | 7 | use simba::simd::{PrimitiveSimdValue, SimdValue};
|
@@ -192,32 +193,47 @@ impl<T: Scalar, const R: usize, const C: usize> From<SMatrix<T, R, C>> for [[T;
|
192 | 193 | }
|
193 | 194 | }
|
194 | 195 |
|
195 |
| -macro_rules! impl_from_into_asref_2D( |
196 |
| - ($(($NRows: ty, $NCols: ty) => ($SZRows: expr, $SZCols: expr));* $(;)*) => {$( |
197 |
| - impl<T: Scalar, S> AsRef<[[T; $SZRows]; $SZCols]> for Matrix<T, $NRows, $NCols, S> |
| 196 | +macro_rules! impl_from_into_asref_borrow_2D( |
| 197 | + |
| 198 | + //does the impls on one case for either AsRef/AsMut and Borrow/BorrowMut |
| 199 | + ( |
| 200 | + ($NRows: ty, $NCols: ty) => ($SZRows: expr, $SZCols: expr); |
| 201 | + $Ref:ident.$ref:ident(), $Mut:ident.$mut:ident() |
| 202 | + ) => { |
| 203 | + impl<T: Scalar, S> $Ref<[[T; $SZRows]; $SZCols]> for Matrix<T, $NRows, $NCols, S> |
198 | 204 | where S: ContiguousStorage<T, $NRows, $NCols> {
|
199 | 205 | #[inline]
|
200 |
| - fn as_ref(&self) -> &[[T; $SZRows]; $SZCols] { |
| 206 | + fn $ref(&self) -> &[[T; $SZRows]; $SZCols] { |
201 | 207 | unsafe {
|
202 | 208 | &*(self.data.ptr() as *const [[T; $SZRows]; $SZCols])
|
203 | 209 | }
|
204 | 210 | }
|
205 | 211 | }
|
206 | 212 |
|
207 |
| - impl<T: Scalar, S> AsMut<[[T; $SZRows]; $SZCols]> for Matrix<T, $NRows, $NCols, S> |
| 213 | + impl<T: Scalar, S> $Mut<[[T; $SZRows]; $SZCols]> for Matrix<T, $NRows, $NCols, S> |
208 | 214 | where S: ContiguousStorageMut<T, $NRows, $NCols> {
|
209 | 215 | #[inline]
|
210 |
| - fn as_mut(&mut self) -> &mut [[T; $SZRows]; $SZCols] { |
| 216 | + fn $mut(&mut self) -> &mut [[T; $SZRows]; $SZCols] { |
211 | 217 | unsafe {
|
212 | 218 | &mut *(self.data.ptr_mut() as *mut [[T; $SZRows]; $SZCols])
|
213 | 219 | }
|
214 | 220 | }
|
215 | 221 | }
|
| 222 | + }; |
| 223 | + |
| 224 | + //collects the mappings from typenum pairs to consts |
| 225 | + ($(($NRows: ty, $NCols: ty) => ($SZRows: expr, $SZCols: expr));* $(;)*) => {$( |
| 226 | + impl_from_into_asref_borrow_2D!( |
| 227 | + ($NRows, $NCols) => ($SZRows, $SZCols); AsRef.as_ref(), AsMut.as_mut() |
| 228 | + ); |
| 229 | + impl_from_into_asref_borrow_2D!( |
| 230 | + ($NRows, $NCols) => ($SZRows, $SZCols); Borrow.borrow(), BorrowMut.borrow_mut() |
| 231 | + ); |
216 | 232 | )*}
|
217 | 233 | );
|
218 | 234 |
|
219 | 235 | // Implement for matrices with shape 2x2 .. 6x6.
|
220 |
| -impl_from_into_asref_2D!( |
| 236 | +impl_from_into_asref_borrow_2D!( |
221 | 237 | (U2, U2) => (2, 2); (U2, U3) => (2, 3); (U2, U4) => (2, 4); (U2, U5) => (2, 5); (U2, U6) => (2, 6);
|
222 | 238 | (U3, U2) => (3, 2); (U3, U3) => (3, 3); (U3, U4) => (3, 4); (U3, U5) => (3, 5); (U3, U6) => (3, 6);
|
223 | 239 | (U4, U2) => (4, 2); (U4, U3) => (4, 3); (U4, U4) => (4, 4); (U4, U5) => (4, 5); (U4, U6) => (4, 6);
|
|
0 commit comments