Skip to content

Commit

Permalink
Add optional bytemuck support, mention #80
Browse files Browse the repository at this point in the history
  • Loading branch information
yoanlcq committed Dec 5, 2021
1 parent c9733e7 commit 7823963
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
2 changes: 1 addition & 1 deletion examples/bin-with-optional-dependencies/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
vek = { path = "../../", features = ["repr_simd", "image", "serde", "mint", "bytemuck"] }
15 changes: 14 additions & 1 deletion examples/bin-with-optional-dependencies/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
extern crate vek;

fn test_bytemuck() {
println!("---- test bytemuck ----");
let v = vek::vec::repr_simd::Vec3::<u32>::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();
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
15 changes: 15 additions & 0 deletions src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, }) => {
Expand Down Expand Up @@ -1617,6 +1620,18 @@ macro_rules! vec_impl_vec {
}
}
*/

#[cfg(feature = "bytemuck")]
unsafe impl<T> bytemuck::Zeroable for $Vec<T> where T: bytemuck::Zeroable {
fn zeroed() -> Self {
Self::new($({ let $namedget = T::zeroed(); $namedget }),+)
}
}

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

Expand Down

0 comments on commit 7823963

Please sign in to comment.