Skip to content

Optional bytemuck support #99

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

Merged
merged 1 commit into from
Oct 19, 2024
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
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