Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cast using bytemuck crate #541

Merged
merged 2 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 10 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,20 @@ macro_rules! impl_mint_conversions {
$ArrayN { $( $field: v.$field, )+ }
}
}

impl<S: Clone> mint::IntoMint for $ArrayN<S> {
type MintType = mint::$Mint<S>;
}
}
}

/// 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"));
9 changes: 8 additions & 1 deletion src/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,7 @@ macro_rules! mint_conversions {
$MatrixN { $($field: m.$field.into()),+ }
}
}

impl<S: Clone> mint::IntoMint for $MatrixN<S> {
type MintType = mint::$MintN<S>;
}
Expand All @@ -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
5 changes: 4 additions & 1 deletion src/quaternion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,10 +686,13 @@ impl<S: Clone> From<Quaternion<S>> for mint::Quaternion<S> {
}

#[cfg(feature = "mint")]
impl <S: Clone> mint::IntoMint for Quaternion<S> {
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
5 changes: 4 additions & 1 deletion src/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,10 @@ where
/// let centroid = Point2::centroid(&triangle);
/// ```
#[inline]
fn centroid(points: &[Self]) -> Self where Self::Scalar: NumCast {
fn centroid(points: &[Self]) -> Self
where
Self::Scalar: NumCast,
{
let total_displacement = points
.iter()
.fold(Self::Diff::zero(), |acc, p| acc + p.to_vec());
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