Add support for cast using bytemuck crate

This commit is contained in:
maku693 2022-02-23 17:32:34 +09:00 committed by Dzmitry Malyshau
parent 78c082e944
commit 11a5346291
8 changed files with 45 additions and 0 deletions

View file

@ -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"

View file

@ -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);

View file

@ -55,6 +55,9 @@
#[macro_use]
extern crate approx;
#[cfg(feature = "bytemuck")]
extern crate bytemuck;
#[cfg(feature = "mint")]
pub extern crate mint;

View file

@ -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"));

View file

@ -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.

View file

@ -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 ")?;

View file

@ -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::*;

View file

@ -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")]