Skip to content

Commit 11a5346

Browse files
maku693kvark
authored andcommitted
Add support for cast using bytemuck crate
1 parent 78c082e commit 11a5346

File tree

8 files changed

+45
-0
lines changed

8 files changed

+45
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ rand = { version = "0.8", features = ["small_rng"], optional = true }
2929
serde = { version = "1.0", features = ["serde_derive"], optional = true }
3030
# works only in rust toolchain up to 1.32, disabled indefinitely
3131
#simd = { version = "0.2", optional = true }
32+
bytemuck = { version = "1.0", optional = true }
3233

3334
[dev-dependencies]
3435
serde_json = "1.0"

src/euler.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,6 @@ impl<S: Clone, A: Angle + Into<S>> From<Euler<A>> for MintEuler<S> {
224224
MintEuler::from([v.x.into(), v.y.into(), v.z.into()])
225225
}
226226
}
227+
228+
#[cfg(feature = "bytemuck")]
229+
impl_bytemuck_cast!(Euler);

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
#[macro_use]
5656
extern crate approx;
5757

58+
#[cfg(feature = "bytemuck")]
59+
extern crate bytemuck;
60+
5861
#[cfg(feature = "mint")]
5962
pub extern crate mint;
6063

src/macros.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,13 @@ macro_rules! impl_mint_conversions {
380380
}
381381
}
382382

383+
/// Generate implementation required to cast using `bytemuck`
384+
#[cfg(feature = "bytemuck")]
385+
macro_rules! impl_bytemuck_cast {
386+
($ArrayN:ident) => {
387+
unsafe impl<S: bytemuck::Pod> bytemuck::Pod for $ArrayN<S> {}
388+
unsafe impl<S: bytemuck::Zeroable> bytemuck::Zeroable for $ArrayN<S> {}
389+
};
390+
}
391+
383392
include!(concat!(env!("OUT_DIR"), "/swizzle_operator_macro.rs"));

src/matrix.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,13 @@ mint_conversions!(Matrix3 { x, y, z }, ColumnMatrix3);
15721572
#[cfg(feature = "mint")]
15731573
mint_conversions!(Matrix4 { x, y, z, w }, ColumnMatrix4);
15741574

1575+
#[cfg(feature = "bytemuck")]
1576+
impl_bytemuck_cast!(Matrix2);
1577+
#[cfg(feature = "bytemuck")]
1578+
impl_bytemuck_cast!(Matrix3);
1579+
#[cfg(feature = "bytemuck")]
1580+
impl_bytemuck_cast!(Matrix4);
1581+
15751582
impl<S: BaseNum> From<Matrix2<S>> for Matrix3<S> {
15761583
/// Clone the elements of a 2-dimensional matrix into the top-left corner
15771584
/// of a 3-dimensional identity matrix.

src/point.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,13 @@ impl_mint_conversions!(Point2 { x, y }, Point2);
368368
#[cfg(feature = "mint")]
369369
impl_mint_conversions!(Point3 { x, y, z }, Point3);
370370

371+
#[cfg(feature = "bytemuck")]
372+
impl_bytemuck_cast!(Point1);
373+
#[cfg(feature = "bytemuck")]
374+
impl_bytemuck_cast!(Point2);
375+
#[cfg(feature = "bytemuck")]
376+
impl_bytemuck_cast!(Point3);
377+
371378
impl<S: fmt::Debug> fmt::Debug for Point1<S> {
372379
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
373380
write!(f, "Point1 ")?;

src/quaternion.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,9 @@ impl <S: Clone> mint::IntoMint for Quaternion<S> {
690690
type MintType = mint::Quaternion<S>;
691691
}
692692

693+
#[cfg(feature = "bytemuck")]
694+
impl_bytemuck_cast!(Quaternion);
695+
693696
#[cfg(test)]
694697
mod tests {
695698
use quaternion::*;

src/vector.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,18 @@ impl<S: fmt::Debug> fmt::Debug for Vector4<S> {
597597
}
598598
}
599599

600+
#[cfg(feature = "bytemuck")]
601+
impl_bytemuck_cast!(Vector1);
602+
603+
#[cfg(feature = "bytemuck")]
604+
impl_bytemuck_cast!(Vector2);
605+
606+
#[cfg(feature = "bytemuck")]
607+
impl_bytemuck_cast!(Vector3);
608+
609+
#[cfg(feature = "bytemuck")]
610+
impl_bytemuck_cast!(Vector4);
611+
600612
#[cfg(feature = "mint")]
601613
impl_mint_conversions!(Vector2 { x, y }, Vector2);
602614
#[cfg(feature = "mint")]

0 commit comments

Comments
 (0)