From 712bfd6e6661ea097421f77f529fff4a71868448 Mon Sep 17 00:00:00 2001 From: Ben Derrett Date: Tue, 18 Jan 2022 08:27:16 +0000 Subject: [PATCH] Impl NdIndex for &T and Vec --- src/dimension/ndindex.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/dimension/ndindex.rs b/src/dimension/ndindex.rs index 718ee059b..c731d30de 100644 --- a/src/dimension/ndindex.rs +++ b/src/dimension/ndindex.rs @@ -219,22 +219,34 @@ impl<'a> IntoDimension for &'a [Ix] { } } -unsafe impl<'a> NdIndex for &'a IxDyn { +unsafe impl<'a> NdIndex for &'a [Ix] { fn index_checked(&self, dim: &IxDyn, strides: &IxDyn) -> Option { - (**self).index_checked(dim, strides) + stride_offset_checked(dim.ix(), strides.ix(), *self) } fn index_unchecked(&self, strides: &IxDyn) -> isize { - (**self).index_unchecked(strides) + zip(strides.ix(), *self) + .map(|(&s, &i)| stride_offset(i, s)) + .sum() } } -unsafe impl<'a> NdIndex for &'a [Ix] { +unsafe impl<'a, T> NdIndex for &T +where + T: NdIndex, +{ fn index_checked(&self, dim: &IxDyn, strides: &IxDyn) -> Option { - stride_offset_checked(dim.ix(), strides.ix(), *self) + (*self).index_checked(dim, strides) } fn index_unchecked(&self, strides: &IxDyn) -> isize { - zip(strides.ix(), *self) - .map(|(&s, &i)| stride_offset(i, s)) - .sum() + (*self).index_unchecked(strides) + } +} + +unsafe impl<'a> NdIndex for Vec { + fn index_checked(&self, dim: &IxDyn, strides: &IxDyn) -> Option { + (&**self).index_checked(dim, strides) + } + fn index_unchecked(&self, strides: &IxDyn) -> isize { + (&**self).index_unchecked(strides) } }