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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for arbitrary #324

Merged
merged 1 commit into from Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -361,6 +361,9 @@ pub mod __private {

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

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

/*
Expand Down