From 11a5346291d8813f946babfb779bd6af4b8c138f Mon Sep 17 00:00:00 2001 From: maku693 Date: Wed, 23 Feb 2022 17:32:34 +0900 Subject: [PATCH] Add support for cast using bytemuck crate --- Cargo.toml | 1 + src/euler.rs | 3 +++ src/lib.rs | 3 +++ src/macros.rs | 9 +++++++++ src/matrix.rs | 7 +++++++ src/point.rs | 7 +++++++ src/quaternion.rs | 3 +++ src/vector.rs | 12 ++++++++++++ 8 files changed, 45 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 82745c6..687a295 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,6 +29,7 @@ rand = { version = "0.8", features = ["small_rng"], optional = true } serde = { version = "1.0", features = ["serde_derive"], optional = true } # works only in rust toolchain up to 1.32, disabled indefinitely #simd = { version = "0.2", optional = true } +bytemuck = { version = "1.0", optional = true } [dev-dependencies] serde_json = "1.0" diff --git a/src/euler.rs b/src/euler.rs index 264271a..2a11b60 100644 --- a/src/euler.rs +++ b/src/euler.rs @@ -224,3 +224,6 @@ impl> From> for MintEuler { MintEuler::from([v.x.into(), v.y.into(), v.z.into()]) } } + +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Euler); diff --git a/src/lib.rs b/src/lib.rs index b9642cb..44febaf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,6 +55,9 @@ #[macro_use] extern crate approx; +#[cfg(feature = "bytemuck")] +extern crate bytemuck; + #[cfg(feature = "mint")] pub extern crate mint; diff --git a/src/macros.rs b/src/macros.rs index cd0cd1b..d044543 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -380,4 +380,13 @@ macro_rules! impl_mint_conversions { } } +/// Generate implementation required to cast using `bytemuck` +#[cfg(feature = "bytemuck")] +macro_rules! impl_bytemuck_cast { + ($ArrayN:ident) => { + unsafe impl bytemuck::Pod for $ArrayN {} + unsafe impl bytemuck::Zeroable for $ArrayN {} + }; +} + include!(concat!(env!("OUT_DIR"), "/swizzle_operator_macro.rs")); diff --git a/src/matrix.rs b/src/matrix.rs index ed96761..254d84e 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -1572,6 +1572,13 @@ mint_conversions!(Matrix3 { x, y, z }, ColumnMatrix3); #[cfg(feature = "mint")] mint_conversions!(Matrix4 { x, y, z, w }, ColumnMatrix4); +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Matrix2); +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Matrix3); +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Matrix4); + impl From> for Matrix3 { /// Clone the elements of a 2-dimensional matrix into the top-left corner /// of a 3-dimensional identity matrix. diff --git a/src/point.rs b/src/point.rs index b65c334..77137dc 100644 --- a/src/point.rs +++ b/src/point.rs @@ -368,6 +368,13 @@ impl_mint_conversions!(Point2 { x, y }, Point2); #[cfg(feature = "mint")] impl_mint_conversions!(Point3 { x, y, z }, Point3); +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Point1); +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Point2); +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Point3); + impl fmt::Debug for Point1 { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Point1 ")?; diff --git a/src/quaternion.rs b/src/quaternion.rs index bf1c2cd..3f1f318 100644 --- a/src/quaternion.rs +++ b/src/quaternion.rs @@ -690,6 +690,9 @@ impl mint::IntoMint for Quaternion { type MintType = mint::Quaternion; } +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Quaternion); + #[cfg(test)] mod tests { use quaternion::*; diff --git a/src/vector.rs b/src/vector.rs index 92a59f9..a569687 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -597,6 +597,18 @@ impl fmt::Debug for Vector4 { } } +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Vector1); + +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Vector2); + +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Vector3); + +#[cfg(feature = "bytemuck")] +impl_bytemuck_cast!(Vector4); + #[cfg(feature = "mint")] impl_mint_conversions!(Vector2 { x, y }, Vector2); #[cfg(feature = "mint")]