Skip to content

Commit

Permalink
Merge pull request #324 from KodrAus/feat/arbitrary
Browse files Browse the repository at this point in the history
Add support for arbitrary
  • Loading branch information
KodrAus committed Apr 3, 2023
2 parents ddc975b + 79e3236 commit 83359a6
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Expand Up @@ -49,7 +49,7 @@ jobs:
run: cargo install cargo-hack

- name: Powerset
run: cargo hack test --feature-powerset --lib --optional-deps "std serde" --depth 3 --skip rustc-dep-of-std
run: cargo hack test --feature-powerset --lib --optional-deps --depth 3 --skip "compiler_builtins core rustc-dep-of-std"

- name: Docs
run: cargo doc --features example_generated
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -21,6 +21,7 @@ exclude = ["tests", ".github"]

[dependencies]
serde = { version = "1.0", optional = true, default-features = false }
arbitrary = { version = "1.0", optional = true }
core = { version = "1.0.0", optional = true, package = "rustc-std-workspace-core" }
compiler_builtins = { version = "0.1.2", optional = true }

Expand All @@ -30,6 +31,7 @@ rustversion = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
serde_test = "1.0"
arbitrary = { version = "1.0", features = ["derive"] }

[features]
std = []
Expand Down
49 changes: 49 additions & 0 deletions src/external.rs
Expand Up @@ -3,6 +3,9 @@
#[cfg(feature = "serde")]
pub mod serde_support;

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

/// Implements traits from external libraries for the internal bitflags type.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
Expand All @@ -27,6 +30,15 @@ macro_rules! __impl_external_bitflags {
)*
}
}

__impl_external_bitflags_arbitrary! {
$InternalBitFlags: $T {
$(
$(#[$attr $($args)*])*
$Flag;
)*
}
}
};
}

Expand Down Expand Up @@ -80,3 +92,40 @@ macro_rules! __impl_external_bitflags_serde {
}
) => {};
}

/// Implement `Arbitrary` for the internal bitflags type.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
#[cfg(feature = "arbitrary")]
macro_rules! __impl_external_bitflags_arbitrary {
(
$InternalBitFlags:ident: $T:ty {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
}
) => {
impl<'a> $crate::__private::arbitrary::Arbitrary<'a> for $InternalBitFlags {
fn arbitrary(
u: &mut $crate::__private::arbitrary::Unstructured<'a>,
) -> $crate::__private::arbitrary::Result<Self> {
Self::from_bits(u.arbitrary()?).ok_or_else(|| $crate::__private::arbitrary::Error::IncorrectFormat)
}
}
};
}

#[macro_export(local_inner_macros)]
#[doc(hidden)]
#[cfg(not(feature = "arbitrary"))]
macro_rules! __impl_external_bitflags_arbitrary {
(
$InternalBitFlags:ident: $T:ty {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
}
) => {};
}
19 changes: 19 additions & 0 deletions src/external/arbitrary_support.rs
@@ -0,0 +1,19 @@
#[cfg(test)]
mod tests {
use arbitrary::Arbitrary;

bitflags! {
#[derive(Arbitrary)]
struct Color: u32 {
const RED = 0x1;
const GREEN = 0x2;
const BLUE = 0x4;
}
}

#[test]
fn test_arbitrary() {
let mut unstructured = arbitrary::Unstructured::new(&[0_u8; 256]);
let _color = Color::arbitrary(&mut unstructured);
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Expand Up @@ -411,6 +411,9 @@ pub mod __private {

#[cfg(feature = "serde")]
pub use serde;

#[cfg(feature = "arbitrary")]
pub use arbitrary;
}

/*
Expand Down

0 comments on commit 83359a6

Please sign in to comment.