Skip to content

Commit

Permalink
Add support for cast using bytemuck crate
Browse files Browse the repository at this point in the history
  • Loading branch information
maku693 authored and kvark committed Mar 7, 2022
1 parent 78c082e commit 11a5346
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
3 changes: 3 additions & 0 deletions src/euler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,6 @@ impl<S: Clone, A: Angle + Into<S>> From<Euler<A>> for MintEuler<S> {
MintEuler::from([v.x.into(), v.y.into(), v.z.into()])
}
}

#[cfg(feature = "bytemuck")]
impl_bytemuck_cast!(Euler);
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
#[macro_use]
extern crate approx;

#[cfg(feature = "bytemuck")]
extern crate bytemuck;

#[cfg(feature = "mint")]
pub extern crate mint;

Expand Down
9 changes: 9 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: bytemuck::Pod> bytemuck::Pod for $ArrayN<S> {}
unsafe impl<S: bytemuck::Zeroable> bytemuck::Zeroable for $ArrayN<S> {}
};
}

include!(concat!(env!("OUT_DIR"), "/swizzle_operator_macro.rs"));
7 changes: 7 additions & 0 deletions src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: BaseNum> From<Matrix2<S>> for Matrix3<S> {
/// Clone the elements of a 2-dimensional matrix into the top-left corner
/// of a 3-dimensional identity matrix.
Expand Down
7 changes: 7 additions & 0 deletions src/point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S: fmt::Debug> fmt::Debug for Point1<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Point1 ")?;
Expand Down
3 changes: 3 additions & 0 deletions src/quaternion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,9 @@ impl <S: Clone> mint::IntoMint for Quaternion<S> {
type MintType = mint::Quaternion<S>;
}

#[cfg(feature = "bytemuck")]
impl_bytemuck_cast!(Quaternion);

#[cfg(test)]
mod tests {
use quaternion::*;
Expand Down
12 changes: 12 additions & 0 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,18 @@ impl<S: fmt::Debug> fmt::Debug for Vector4<S> {
}
}

#[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")]
Expand Down

0 comments on commit 11a5346

Please sign in to comment.