Skip to content

Commit 16fb130

Browse files
committed
Impl arbitrary::Arbitrary for Array
Support for creating arbitrary arrays using rust-fuzz's `arbitrary` crate, which is useful for randomized testing
1 parent 0b65719 commit 16fb130

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

.github/workflows/hybrid-array.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ jobs:
3838
targets: ${{ matrix.target }}
3939
- uses: RustCrypto/actions/cargo-hack-install@master
4040
- run: cargo hack build --target ${{ matrix.target }} --feature-powerset --optional-deps bytemuck,serde,subtle,zeroize
41-
- run: cargo build --all-features --release
4241

4342
careful:
4443
runs-on: ubuntu-latest
@@ -106,5 +105,5 @@ jobs:
106105
with:
107106
toolchain: ${{ matrix.toolchain }}
108107
- uses: RustCrypto/actions/cargo-hack-install@master
109-
- run: cargo hack test --feature-powerset --optional-deps bytemuck,serde,subtle,zeroize
108+
- run: cargo hack test --feature-powerset --optional-deps arbitrary,bytemuck,serde,subtle,zeroize
110109
- run: cargo test --all-features --release

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ rust-version = "1.85"
2020
typenum = { version = "1.17", features = ["const-generics"] }
2121

2222
# optional dependencies
23+
arbitrary = { version = "1", optional = true }
2324
bytemuck = { version = "1", optional = true, default-features = false }
2425
serde = { version = "1", optional = true, default-features = false }
2526
subtle = { version = "2", optional = true, default-features = false, features = ["const-generics"] }

src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ use core::{
145145
};
146146
use typenum::{Diff, Sum};
147147

148+
#[cfg(feature = "arbitrary")]
149+
use arbitrary::Arbitrary;
150+
148151
#[cfg(feature = "bytemuck")]
149152
use bytemuck::{Pod, Zeroable};
150153

@@ -1028,6 +1031,17 @@ where
10281031
}
10291032
}
10301033

1034+
#[cfg(feature = "arbitrary")]
1035+
impl<'a, T, U> Arbitrary<'a> for Array<T, U>
1036+
where
1037+
T: Arbitrary<'a>,
1038+
U: ArraySize,
1039+
{
1040+
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
1041+
Self::try_from_fn(|_n| Arbitrary::arbitrary(u))
1042+
}
1043+
}
1044+
10311045
#[cfg(feature = "bytemuck")]
10321046
unsafe impl<T, U> Pod for Array<T, U>
10331047
where

0 commit comments

Comments
 (0)