From c667b1f9c80d5859acf7515394c9892680357596 Mon Sep 17 00:00:00 2001 From: iMplode nZ Date: Wed, 10 Feb 2021 20:12:09 -0800 Subject: [PATCH 1/5] Added bytemuck implementations for static storages. --- Cargo.toml | 2 +- src/base/matrix.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 7d5a9db9..3fd9ae81 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,6 @@ compare = [ "matrixcompare-core" ] libm = [ "simba/libm" ] libm-force = [ "simba/libm_force" ] - [dependencies] typenum = "1.12" generic-array = "0.14" @@ -55,6 +54,7 @@ mint = { version = "0.5", optional = true } quickcheck = { version = "0.9", optional = true } pest = { version = "2", optional = true } pest_derive = { version = "2", optional = true } +bytemuck = { version = "1.5", optional = true } matrixcompare-core = { version = "0.1", optional = true } [dev-dependencies] diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 8035d2f8..73001536 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -279,6 +279,25 @@ impl> matrixcompare_core::DenseAc } } +#[cfg(feature = "bytemuck")] +unsafe impl + bytemuck::Zeroable for Matrix> +where + R::Value: core::ops::Mul, + >::Output: generic_array::ArrayLength, +{ +} + +#[cfg(feature = "bytemuck")] +unsafe impl + bytemuck::Pod for Matrix> +where + R::Value: core::ops::Mul, + >::Output: generic_array::ArrayLength, + <::Value>>::Output as generic_array::ArrayLength>::ArrayType: Copy +{ +} + impl Matrix { /// Creates a new matrix with the given data without statically checking that the matrix /// dimension matches the storage dimension. From 1c0891bbbb06a09ab2b3000463965b95c92b53bc Mon Sep 17 00:00:00 2001 From: iMplode nZ Date: Fri, 12 Feb 2021 15:30:12 -0800 Subject: [PATCH 2/5] Added bytemuck for Unit and Quaternion. --- src/base/unit.rs | 6 ++++++ src/geometry/quaternion.rs | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/base/unit.rs b/src/base/unit.rs index 2483307a..70e3a927 100644 --- a/src/base/unit.rs +++ b/src/base/unit.rs @@ -30,6 +30,12 @@ pub struct Unit { pub(crate) value: T, } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for Unit where T: bytemuck::Zeroable {} + +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Pod for Unit where T: bytemuck::Pod {} + #[cfg(feature = "serde-serialize")] impl Serialize for Unit { fn serialize(&self, serializer: S) -> Result diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index a5db1c69..78b95332 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -40,6 +40,12 @@ impl Default for Quaternion { } } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for Quaternion where Vector4: bytemuck::Zeroable {} + +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Pod for Quaternion where Vector4: bytemuck::Pod, N: Copy {} + #[cfg(feature = "abomonation-serialize")] impl Abomonation for Quaternion where From dc15261ec15da0797dad8d3687b247db2c26ca4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Thu, 25 Feb 2021 14:10:34 +0100 Subject: [PATCH 3/5] Move the bytemuck impls to GenericArray and add a transitive impl for matrices. --- src/base/array_storage.rs | 20 ++++++++++++++++++++ src/base/matrix.rs | 15 ++++++--------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/base/array_storage.rs b/src/base/array_storage.rs index 4fdd3e73..e067cb49 100644 --- a/src/base/array_storage.rs +++ b/src/base/array_storage.rs @@ -394,6 +394,26 @@ where } } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable + for ArrayStorage +where + R::Value: Mul, + Prod: ArrayLength, + Self: Copy, +{ +} + +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Pod + for ArrayStorage +where + R::Value: Mul, + Prod: ArrayLength, + Self: Copy, +{ +} + #[cfg(feature = "abomonation-serialize")] impl Abomonation for ArrayStorage where diff --git a/src/base/matrix.rs b/src/base/matrix.rs index 73001536..eb525b14 100644 --- a/src/base/matrix.rs +++ b/src/base/matrix.rs @@ -280,21 +280,18 @@ impl> matrixcompare_core::DenseAc } #[cfg(feature = "bytemuck")] -unsafe impl - bytemuck::Zeroable for Matrix> +unsafe impl> bytemuck::Zeroable + for Matrix where - R::Value: core::ops::Mul, - >::Output: generic_array::ArrayLength, + S: bytemuck::Zeroable, { } #[cfg(feature = "bytemuck")] -unsafe impl - bytemuck::Pod for Matrix> +unsafe impl> bytemuck::Pod for Matrix where - R::Value: core::ops::Mul, - >::Output: generic_array::ArrayLength, - <::Value>>::Output as generic_array::ArrayLength>::ArrayType: Copy + S: bytemuck::Pod, + Self: Copy, { } From 0b1e6f0b05f862efb225ad4ab317348ecedbdf09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Thu, 25 Feb 2021 14:16:57 +0100 Subject: [PATCH 4/5] Run cargo fmt. --- src/geometry/quaternion.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/geometry/quaternion.rs b/src/geometry/quaternion.rs index 78b95332..ebed6779 100755 --- a/src/geometry/quaternion.rs +++ b/src/geometry/quaternion.rs @@ -44,7 +44,12 @@ impl Default for Quaternion { unsafe impl bytemuck::Zeroable for Quaternion where Vector4: bytemuck::Zeroable {} #[cfg(feature = "bytemuck")] -unsafe impl bytemuck::Pod for Quaternion where Vector4: bytemuck::Pod, N: Copy {} +unsafe impl bytemuck::Pod for Quaternion +where + Vector4: bytemuck::Pod, + N: Copy, +{ +} #[cfg(feature = "abomonation-serialize")] impl Abomonation for Quaternion From af448d2c707dd9b6d63a157131725e135ff19cf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crozet=20S=C3=A9bastien?= Date: Thu, 25 Feb 2021 14:19:20 +0100 Subject: [PATCH 5/5] Add bytemuck impls to points. --- src/geometry/point.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/geometry/point.rs b/src/geometry/point.rs index 75410ccd..390db80b 100644 --- a/src/geometry/point.rs +++ b/src/geometry/point.rs @@ -65,6 +65,24 @@ where { } +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Zeroable for Point +where + VectorN: bytemuck::Zeroable, + DefaultAllocator: Allocator, +{ +} + +#[cfg(feature = "bytemuck")] +unsafe impl bytemuck::Pod for Point +where + N: Copy, + VectorN: bytemuck::Pod, + DefaultAllocator: Allocator, + >::Buffer: Copy, +{ +} + #[cfg(feature = "serde-serialize")] impl Serialize for Point where