From b044907bd258c4ac2a65017c7a0365188c4770cf Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon <76854355+carloskiki@users.noreply.github.com> Date: Sat, 17 Jan 2026 08:36:39 -0500 Subject: [PATCH 1/3] implement `zerocopy` traits for `Array` --- Cargo.lock | 25 +++++++++++++++++++++++-- Cargo.toml | 1 + src/lib.rs | 7 +++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0785d0a..d5d4e30 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -23,6 +23,7 @@ dependencies = [ "serde", "subtle", "typenum", + "zerocopy", "zeroize", ] @@ -37,9 +38,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.39" +version = "1.0.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1f1914ce909e1658d9907913b4b91947430c7d9be598b15a1912935b8c04801" +checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a" dependencies = [ "proc-macro2", ] @@ -93,6 +94,26 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" +[[package]] +name = "zerocopy" +version = "0.8.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "668f5168d10b9ee831de31933dc111a459c97ec93225beb307aed970d1372dfd" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c7962b26b0a8685668b671ee4b54d007a67d4eaf05fda79ac0ecf41e32270f1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "zeroize" version = "1.8.2" diff --git a/Cargo.toml b/Cargo.toml index 7a122f3..9a64d46 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ bytemuck = { version = "1", optional = true, default-features = false } serde = { version = "1", optional = true, default-features = false } subtle = { version = "2", optional = true, default-features = false, features = ["const-generics"] } zeroize = { version = "1.8", optional = true, default-features = false } +zerocopy = { version = "0.8", optional = true, features = ["derive"] } [features] alloc = [] diff --git a/src/lib.rs b/src/lib.rs index 67a8da0..9bcd76f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -157,6 +157,9 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq}; #[cfg(feature = "zeroize")] use zeroize::{Zeroize, ZeroizeOnDrop}; +#[cfg(feature = "zerocopy")] +use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned}; + /// Type alias for [`Array`] which is const generic around a size `N`, ala `[T; N]`. pub type ArrayN = Array::Size>; @@ -174,6 +177,10 @@ pub type ArrayN = Array::Size>; /// /// let arr: Array = Array([1, 2, 3]); /// ``` +#[cfg_attr( + feature = "zerocopy", + derive(IntoBytes, FromBytes, Immutable, Unaligned, KnownLayout) +)] #[repr(transparent)] pub struct Array(pub U::ArrayType); From e858ddd2135de3a5f7ca0c791bbac39589bd20cb Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon <76854355+carloskiki@users.noreply.github.com> Date: Sat, 17 Jan 2026 08:36:51 -0500 Subject: [PATCH 2/3] test the traits --- tests/mod.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/mod.rs b/tests/mod.rs index aad7cd3..efd618e 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -154,3 +154,13 @@ fn slice_as_flattened() { ); assert_eq!(Array::slice_as_flattened(slice), &[1, 2, 3, 4, 5, 6, 7, 8]); } + +#[test] +#[cfg(feature = "zerocopy")] +#[allow(unused)] +fn zerocopy_traits() { + use zerocopy::{IntoBytes, FromBytes, Unaligned, Immutable, KnownLayout}; + struct Check(T); + let ok: Check::> = Check(Array([1, 2, 3, 4, 5])); + // let not_unaligned: Check::> = Check(Array([1, 2, 3, 4, 5])); +} From 3e685b3daffc8980d0c3cf32db2bf2db36c146bc Mon Sep 17 00:00:00 2001 From: Charles Edward Gagnon <76854355+carloskiki@users.noreply.github.com> Date: Sat, 17 Jan 2026 08:38:16 -0500 Subject: [PATCH 3/3] chore: fmt --- tests/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/mod.rs b/tests/mod.rs index efd618e..ae56d4f 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -159,8 +159,8 @@ fn slice_as_flattened() { #[cfg(feature = "zerocopy")] #[allow(unused)] fn zerocopy_traits() { - use zerocopy::{IntoBytes, FromBytes, Unaligned, Immutable, KnownLayout}; + use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned}; struct Check(T); - let ok: Check::> = Check(Array([1, 2, 3, 4, 5])); + let ok: Check> = Check(Array([1, 2, 3, 4, 5])); // let not_unaligned: Check::> = Check(Array([1, 2, 3, 4, 5])); }