Skip to content

Commit

Permalink
Add optional "arbitrary" crate integration
Browse files Browse the repository at this point in the history
  • Loading branch information
morr0ne committed Jun 19, 2024
1 parent fa1daac commit 7f264e3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ default = ["std"]
std = []

[dependencies]
arbitrary = { version = "1.1.6", optional = true }
serde = { version = "1.0.60", optional = true, default-features = false, features = ["alloc"] }

[dev-dependencies]
Expand Down
18 changes: 18 additions & 0 deletions src/arbitrary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::{Bytes, BytesMut};
use arbitrary::{Arbitrary, Result};

impl<'a> Arbitrary<'a> for Bytes {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> Result<Self> {
let len = u.arbitrary_len::<u8>()?;

u.bytes(len).map(Bytes::copy_from_slice)
}
}

impl<'a> Arbitrary<'a> for BytesMut {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> Result<Self> {
let len = u.arbitrary_len::<u8>()?;

u.bytes(len).map(|slice| BytesMut::from_vec(slice.to_vec()))
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ pub use crate::bytes_mut::BytesMut;
#[cfg(feature = "serde")]
mod serde;

#[cfg(feature = "arbitrary")]
mod arbitrary;

#[inline(never)]
#[cold]
fn abort() -> ! {
Expand Down

0 comments on commit 7f264e3

Please sign in to comment.