diff --git a/Cargo.toml b/Cargo.toml index 2fad2b0..91045b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -60,4 +60,5 @@ static_assertions = "1.1.0" image = { version = "0.23", optional = true, default-features = false } serde = { version = "1.0.105", 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 } diff --git a/examples/bin-with-optional-dependencies/Cargo.toml b/examples/bin-with-optional-dependencies/Cargo.toml index bd7af29..7607582 100644 --- a/examples/bin-with-optional-dependencies/Cargo.toml +++ b/examples/bin-with-optional-dependencies/Cargo.toml @@ -6,4 +6,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -vek = { path = "../../", features = ["repr_simd", "image", "serde", "mint"] } \ No newline at end of file +vek = { path = "../../", features = ["repr_simd", "image", "serde", "mint", "bytemuck"] } \ No newline at end of file diff --git a/examples/bin-with-optional-dependencies/src/main.rs b/examples/bin-with-optional-dependencies/src/main.rs index e7a11a9..50d8657 100644 --- a/examples/bin-with-optional-dependencies/src/main.rs +++ b/examples/bin-with-optional-dependencies/src/main.rs @@ -1,3 +1,16 @@ +extern crate vek; + +fn test_bytemuck() { + println!("---- test bytemuck ----"); + let v = vek::vec::repr_simd::Vec3::::iota(); + let vslice = &[v]; + let data: &[u8] = vek::bytemuck::cast_slice(vslice); + println!("vec : {}", v); + println!("size: {}", std::mem::size_of_val(vslice)); + println!("data: {:?}", data); + println!("---- end test bytemuck ----"); +} + fn main() { - println!("Hello, world!"); + test_bytemuck(); } diff --git a/src/lib.rs b/src/lib.rs index 3de8e40..f4edb3a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,9 @@ pub extern crate serde; #[cfg(feature = "mint")] pub extern crate mint; +#[cfg(feature = "bytemuck")] +pub extern crate bytemuck; + pub extern crate num_integer; pub extern crate num_traits; diff --git a/src/vec.rs b/src/vec.rs index 9f26c07..d7b3e83 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -20,6 +20,9 @@ use crate::ops::*; #[cfg(feature = "platform_intrinsics")] use crate::simd_llvm; +#[cfg(feature = "bytemuck")] +use crate::bytemuck; + // Macro for selecting separate implementations for repr(C) vs repr(simd), at compile time. macro_rules! choose { (c { c => $c_impl:expr, simd_llvm => $s_impl:expr, }) => { @@ -1617,6 +1620,18 @@ macro_rules! vec_impl_vec { } } */ + + #[cfg(feature = "bytemuck")] + unsafe impl bytemuck::Zeroable for $Vec where T: bytemuck::Zeroable { + fn zeroed() -> Self { + Self::new($({ let $namedget = T::zeroed(); $namedget }),+) + } + } + + #[cfg(feature = "bytemuck")] + unsafe impl bytemuck::Pod for $Vec where T: bytemuck::Pod { + // Nothing here + } }; }