From 8383517ba37d8e4c1adfcbf39473fb6cd1afc7f1 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Mon, 15 Mar 2021 20:18:19 +0100 Subject: [PATCH] ensure slice can be cast to Matrix --- sbp/src/operators/algos/constmatrix.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/sbp/src/operators/algos/constmatrix.rs b/sbp/src/operators/algos/constmatrix.rs index 1f38d02..1cf2429 100644 --- a/sbp/src/operators/algos/constmatrix.rs +++ b/sbp/src/operators/algos/constmatrix.rs @@ -4,7 +4,7 @@ use num_traits::identities::Zero; /// A row-major matrix #[derive(Debug, Clone, PartialEq, Eq, Hash)] -#[repr(C)] +#[repr(transparent)] pub struct Matrix { pub data: [[T; N]; M], } @@ -346,4 +346,18 @@ mod flipping { let flipped = m.flip_ud(); assert_eq!(flipped, Matrix::new([[5.0], [4.0], [3.0], [2.0], [1.0]])); } + + #[test] + fn assert_castability_of_alignment() { + let m = Matrix::new([[1.0], [2.0], [3.0], [4.0_f64]]); + assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::()); + let m = Matrix::new([[1.0], [2.0], [3.0], [4.0_f32]]); + assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::()); + let m = Matrix::new([[1], [2], [3], [4_i32]]); + assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::()); + let m = Matrix::new([[1], [2], [3], [4_u64]]); + assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::()); + let m = Matrix::new([[1], [2], [3], [4_u128]]); + assert_eq!(std::mem::align_of_val(&m), std::mem::align_of::()); + } }