Skip to content

Commit

Permalink
Optional bytemuck support (#99)
Browse files Browse the repository at this point in the history
Adds impls of `Pod` and `Zeroable` for `Array`s of types which impl
these traits respectively.

Closes #83
  • Loading branch information
tarcieri authored Oct 19, 2024
1 parent 5275b8f commit 2ca79c5
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ rust-version = "1.81"
typenum = { version = "1.17", features = ["const-generics"] }

# optional dependencies
bytemuck = { version = "1", optional = true, default-features = false }
serde = { version = "1", optional = true, default-features = false }
zeroize = { version = "1.8", optional = true, default-features = false }

Expand Down
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ possible with the stable implementation of const generics:
Internally the crate is built on const generics and provides traits which make
it possible to convert between const generic types and `typenum` types.

## Features

This crate exposes the following feature flags. The default is NO features.

- `serde`: implements the `Deserialize` and `Serialize` traits for `Array`
- `zeroize`: implements [`Zeroize`](https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html) for `Array<T: Zeroize, U>`

## License

Licensed under either of:
Expand Down
28 changes: 28 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@
unused_qualifications
)]

//! ## Features
//!
//! This crate exposes the following feature flags. The default is NO features.
//!
//! - `bytemuck`: impls the `Pod` and `Zeroable` traits
//! - `serde`: impls the `Deserialize` and `Serialize` traits for `Array`
//! - `zeroize`: impls [`Zeroize`](https://docs.rs/zeroize/latest/zeroize/trait.Zeroize.html) for `Array<T: Zeroize, U>`
//!
//! ## Usage
//!
//! The two core types in this crate are as follows:
Expand Down Expand Up @@ -123,6 +131,9 @@ use core::{
};
use typenum::{Diff, Sum};

#[cfg(feature = "bytemuck")]
use bytemuck::{Pod, Zeroable};

#[cfg(feature = "zeroize")]
use zeroize::{Zeroize, ZeroizeOnDrop};

Expand Down Expand Up @@ -816,6 +827,23 @@ where
}
}

#[cfg(feature = "bytemuck")]
unsafe impl<T, U> Pod for Array<T, U>
where
T: Pod,
U: ArraySize,
U::ArrayType<T>: Copy,
{
}

#[cfg(feature = "bytemuck")]
unsafe impl<T, U> Zeroable for Array<T, U>
where
T: Zeroable,
U: ArraySize,
{
}

#[cfg(feature = "zeroize")]
impl<T, U> Zeroize for Array<T, U>
where
Expand Down

0 comments on commit 2ca79c5

Please sign in to comment.