Skip to content

Commit

Permalink
Merge pull request #92 from jedjoud10/master
Browse files Browse the repository at this point in the history
Implement ``bytemuck`` traits for other common types
  • Loading branch information
yoanlcq authored Jul 16, 2023
2 parents aa23fef + a60f960 commit b5030c6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ approx = { version = "0.5.0", default-features = false }
num-traits = { version = "0.2.15", default-features = false }
num-integer = { version = "0.1.44", default-features = false }
image = { version = "0.23", optional = true, default-features = false }
serde = { version = "1.0.105", optional = true, default-features = false, features = ["derive"] }
serde = { version = "1.0.171", optional = true, default-features = false, features = ["derive"] }
mint = { version = "0.5.4", optional = true }
bytemuck = { version = "1.7.2", optional = true }
# clippy = { version = "0.0.166", optional = true }
Expand Down
19 changes: 19 additions & 0 deletions src/mat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use crate::geom::{Rect, FrustumPlanes}; // NOTE: Rect is therefore always repr_c
use crate::quaternion;
use crate::transform;

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

// Needed because mem::transmute() isn't clever enough to figure out that e.g [T; 16] and [[T; 4]; 4]
// always have the exact same size and layout, regardless of T. The opposite is impossible.
// See https://github.com/rust-lang/rust/issues/47966
Expand Down Expand Up @@ -1432,6 +1435,22 @@ macro_rules! mat_impl_mat {
true
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::Zeroable for $Mat<T> where T: bytemuck::Zeroable, $Vec: bytemuck::Zeroable {
fn zeroed() -> Self {
Self {
$lines: $CVec {
$($get: $Vec::zeroed(),)+
}
}
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::Pod for Mat4<T> where T: bytemuck::Pod {
// Nothing here
}

};
}
Expand Down
21 changes: 21 additions & 0 deletions src/quaternion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ use crate::ops::*;
use std::ops::Add;
use std::ops::*;

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

macro_rules! impl_mul_by_vec {
($Vec3:ident $Vec4:ident) => {
/// 3D vectors can be rotated by being premultiplied by a quaternion, **assuming the
Expand Down Expand Up @@ -733,6 +736,24 @@ macro_rules! quaternion_complete_mod {
&& T::relative_eq(&self.z, &other.z, epsilon, max_relative)
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::Zeroable for Quaternion<T> where T: bytemuck::Zeroable, Vec4<T>: bytemuck::Zeroable {
fn zeroed() -> Self {
Self {
x: T::zeroed(),
y: T::zeroed(),
z: T::zeroed(),
w: T::zeroed(),
}
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::Pod for Mat4<T> where T: bytemuck::Pod {
// Nothing here
}

};
}

Expand Down
9 changes: 9 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3807,6 +3807,14 @@ mod tests {
assert_eq!((2 as $T) + v, v + (2 as $T));
}
};
(repr_simd_padding $Vec:ident<$T:ident>) => {
#[test]
fn test_simd_padding() {
let count = $Vec::<$T>::ELEM_COUNT;
let elem_size = std::mem::size_of::<$T>();
assert_eq!(std::mem::size_of::<$Vec<$T>>(), count * elem_size);
}
};
}
macro_rules! for_each_type {
($vec:ident $Vec:ident $($T:ident)+) => {
Expand All @@ -3829,6 +3837,7 @@ mod tests {
use $crate::vec::repr_simd::$Vec;
test_vec_t!{repr_simd $Vec<$T>}
test_vec_t!{repr_simd_except_bool $Vec<$T>}
test_vec_t!{repr_simd_padding $Vec<$T>}
})+
mod bool {
use $crate::vec::repr_simd::$Vec;
Expand Down

0 comments on commit b5030c6

Please sign in to comment.