Skip to content
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

Add optional support for the arbitrary crate #715

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
Loading