From 739007dfb10332ce1606478a86e26774c6cdd356 Mon Sep 17 00:00:00 2001 From: Maxime Augier Date: Tue, 9 Feb 2021 10:39:39 +0100 Subject: [PATCH 1/2] Derive PartialEq and Eq for multiarray --- src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 73069b9..a514dbb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,7 @@ use std::ops::{ Index, IndexMut }; /// Helper type to wrap things. This helps avoiding trait coherency issues /// w.r.t. `AsRef` and `From`. -#[derive(Copy,Clone)] +#[derive(Copy,Clone,PartialEq,Eq)] pub struct Wrapped(pub T); impl From for Wrapped<[T; 1]> { @@ -65,10 +65,10 @@ impl> AsMut for Wrapped { /// memory layout of a multi-dimensional array. pub unsafe trait LayoutHelper { /// type for a small fixed-size array of isize - type I: AsRef<[isize]> + AsMut<[isize]> + Copy + Clone; + type I: AsRef<[isize]> + AsMut<[isize]> + Copy + Clone + PartialEq + Eq; /// type for a small fixed-size array of usize - type U: AsRef<[usize]> + AsMut<[usize]> + Copy + Clone; + type U: AsRef<[usize]> + AsMut<[usize]> + Copy + Clone + PartialEq + Eq; /// length of the fixed-size arrays this type can create fn dimensions() -> usize; @@ -114,6 +114,7 @@ declare_int_array_maker! { Dim4, 4, [0,0,0,0], Dim3 } declare_int_array_maker! { Dim5, 5, [0,0,0,0,0], Dim4 } declare_int_array_maker! { Dim6, 6, [0,0,0,0,0,0], Dim5 } +#[derive(PartialEq, Eq)] struct MultiArrayLayout where A: LayoutHelper { extents: A::U, steps: A::I, @@ -300,6 +301,7 @@ pub struct MultiArrayRefMut<'a, T: 'a, A> where A: LayoutHelper { /// matrix[[1,0]] = 3; matrix[[1,1]] = 4; /// matrix[[2,0]] = 5; matrix[[2,1]] = 6; /// ``` +#[derive(PartialEq,Eq)] pub struct MultiArray where A: LayoutHelper { layout: MultiArrayLayout, data: Box<[T]>, From a661bb77049811f62dcab2382094e837ed9c6c1c Mon Sep 17 00:00:00 2001 From: Maxime Augier Date: Tue, 9 Feb 2021 11:06:56 +0100 Subject: [PATCH 2/2] Welp, work around autoderive shortcomings --- src/lib.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index a514dbb..e4d6804 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -114,7 +114,7 @@ declare_int_array_maker! { Dim4, 4, [0,0,0,0], Dim3 } declare_int_array_maker! { Dim5, 5, [0,0,0,0,0], Dim4 } declare_int_array_maker! { Dim6, 6, [0,0,0,0,0,0], Dim5 } -#[derive(PartialEq, Eq)] +#[derive(Eq)] struct MultiArrayLayout where A: LayoutHelper { extents: A::U, steps: A::I, @@ -128,6 +128,12 @@ impl Clone for MultiArrayLayout where A: LayoutHelper { } } +impl PartialEq for MultiArrayLayout where A: LayoutHelper { + fn eq(&self, other: &Self) -> bool { + self.extents.eq(&other.extents) && self.steps.eq(&other.steps) + } +} + fn c_array_layout(extents: &[usize], steps: &mut [isize]) -> usize { let dim = extents.len(); assert!(dim == steps.len()); @@ -301,12 +307,18 @@ pub struct MultiArrayRefMut<'a, T: 'a, A> where A: LayoutHelper { /// matrix[[1,0]] = 3; matrix[[1,1]] = 4; /// matrix[[2,0]] = 5; matrix[[2,1]] = 6; /// ``` -#[derive(PartialEq,Eq)] +#[derive(Eq)] pub struct MultiArray where A: LayoutHelper { layout: MultiArrayLayout, data: Box<[T]>, } +impl PartialEq for MultiArray where T: PartialEq, A: LayoutHelper { + fn eq(&self, other: &Self) -> bool { + self.layout.eq(&other.layout) && self.data.eq(&other.data) + } +} + /// Shared view of a 1D array pub type Array1DRef<'a, T> = MultiArrayRef<'a, T, Dim1>; /// Shared view of a 2D array