Skip to content

Commit

Permalink
Merge pull request #355 from KodrAus/fix/self-in-flags
Browse files Browse the repository at this point in the history
Fix Self in flags value expressions
  • Loading branch information
KodrAus committed May 17, 2023
2 parents 31d3e4a + 64fc276 commit 32d406a
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 421 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,23 @@ jobs:
- name: Default features
run: cross test --target mips-unknown-linux-gnu

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab

- name: Install Clippy
run: |
rustup update beta
rustup component add clippy --toolchain beta
- name: Default features
run: |
cd ./tests/smoke-test
cargo +beta clippy
embedded:
name: Build (embedded)
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion src/example_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ __impl_public_bitflags_forward! {
}

__impl_public_bitflags_iter! {
Flags
Flags: u32, Flags
}

__impl_public_bitflags_consts! {
Expand Down
140 changes: 71 additions & 69 deletions src/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Next, define a macro like so:
#[cfg(feature = "serde")]
macro_rules! __impl_external_bitflags_my_library {
(
$InternalBitFlags:ident: $T:ty {
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
Expand All @@ -35,7 +35,7 @@ macro_rules! __impl_external_bitflags_my_library {
#[cfg(not(feature = "my_library"))]
macro_rules! __impl_external_bitflags_my_library {
(
$InternalBitFlags:ident: $T:ty {
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
Expand All @@ -55,7 +55,7 @@ Now, we add our macro call to the `__impl_external_bitflags` macro body:
```rust
__impl_external_bitflags_my_library! {
$InternalBitFlags: $T {
$InternalBitFlags: $T, $PublicBitFlags {
$(
$(#[$attr $($args)*])*
$Flag;
Expand All @@ -76,6 +76,51 @@ pub(crate) mod __private {
pub use bytemuck;
}

/// Implements traits from external libraries for the internal bitflags type.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __impl_external_bitflags {
(
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
}
) => {
// Any new library traits impls should be added here
// Use `serde` as an example: generate code when the feature is available,
// and a no-op when it isn't

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

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

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

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

Expand All @@ -85,11 +130,11 @@ pub mod serde;
#[cfg(feature = "serde")]
macro_rules! __impl_external_bitflags_serde {
(
$InternalBitFlags:ident: $T:ty {
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
)*
}
) => {
impl $crate::__private::serde::Serialize for $InternalBitFlags {
Expand All @@ -98,7 +143,7 @@ macro_rules! __impl_external_bitflags_serde {
serializer: S,
) -> $crate::__private::core::result::Result<S::Ok, S::Error> {
$crate::serde::serialize(
self,
&$PublicBitFlags::from_bits_retain(self.bits()),
serializer,
)
}
Expand All @@ -108,9 +153,11 @@ macro_rules! __impl_external_bitflags_serde {
fn deserialize<D: $crate::__private::serde::Deserializer<'de>>(
deserializer: D,
) -> $crate::__private::core::result::Result<Self, D::Error> {
$crate::serde::deserialize(
let flags: $PublicBitFlags = $crate::serde::deserialize(
deserializer,
)
)?;

Ok(flags.0)
}
}
};
Expand All @@ -121,11 +168,11 @@ macro_rules! __impl_external_bitflags_serde {
#[cfg(not(feature = "serde"))]
macro_rules! __impl_external_bitflags_serde {
(
$InternalBitFlags:ident: $T:ty {
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
)*
}
) => {};
}
Expand All @@ -136,58 +183,13 @@ pub mod arbitrary;
#[cfg(feature = "bytemuck")]
mod bytemuck;

/// Implements traits from external libraries for the internal bitflags type.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __impl_external_bitflags {
(
$InternalBitFlags:ident: $T:ty {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
}
) => {
// Any new library traits impls should be added here
// Use `serde` as an example: generate code when the feature is available,
// and a no-op when it isn't

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

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

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

/// 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 {
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
Expand All @@ -198,7 +200,7 @@ macro_rules! __impl_external_bitflags_arbitrary {
fn arbitrary(
u: &mut $crate::__private::arbitrary::Unstructured<'a>,
) -> $crate::__private::arbitrary::Result<Self> {
$crate::arbitrary::arbitrary(u)
$crate::arbitrary::arbitrary::<$PublicBitFlags>(u).map(|flags| flags.0)
}
}
};
Expand All @@ -209,12 +211,12 @@ macro_rules! __impl_external_bitflags_arbitrary {
#[cfg(not(feature = "arbitrary"))]
macro_rules! __impl_external_bitflags_arbitrary {
(
$InternalBitFlags:ident: $T:ty {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
}
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
}
) => {};
}

Expand All @@ -224,11 +226,11 @@ macro_rules! __impl_external_bitflags_arbitrary {
#[cfg(feature = "bytemuck")]
macro_rules! __impl_external_bitflags_bytemuck {
(
$InternalBitFlags:ident: $T:ty {
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
$Flag:ident;
)*
}
) => {
// SAFETY: $InternalBitFlags is guaranteed to have the same ABI as $T,
Expand Down Expand Up @@ -256,11 +258,11 @@ macro_rules! __impl_external_bitflags_bytemuck {
#[cfg(not(feature = "bytemuck"))]
macro_rules! __impl_external_bitflags_bytemuck {
(
$InternalBitFlags:ident: $T:ty {
$InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
)*
$Flag:ident;
)*
}
) => {};
}
29 changes: 3 additions & 26 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,27 +98,16 @@ macro_rules! __impl_internal_bitflags {
// The internal flags type offers a similar API to the public one

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

__impl_public_bitflags_consts! {
$InternalBitFlags: $T {
$(
$(#[$attr $($args)*])*
#[allow(
dead_code,
deprecated,
unused_attributes,
non_upper_case_globals
)]
$Flag = $value;
)*
}
__impl_public_bitflags_iter! {
$InternalBitFlags: $T, $PublicBitFlags
}

impl $InternalBitFlags {
Expand All @@ -127,18 +116,6 @@ macro_rules! __impl_internal_bitflags {
pub fn bits_mut(&mut self) -> &mut $T {
&mut self.0
}

/// Iterate over enabled flag values.
#[inline]
pub const fn iter(&self) -> $crate::iter::Iter<$PublicBitFlags> {
$crate::iter::Iter::__private_const_new(<$PublicBitFlags as $crate::Flags>::FLAGS, $PublicBitFlags::from_bits_retain(self.0), $PublicBitFlags::from_bits_retain(self.0))
}

/// Iterate over enabled flag values with their stringified names.
#[inline]
pub const fn iter_names(&self) -> $crate::iter::IterNames<$PublicBitFlags> {
$crate::iter::IterNames::__private_const_new(<$PublicBitFlags as $crate::Flags>::FLAGS, $PublicBitFlags::from_bits_retain(self.0), $PublicBitFlags::from_bits_retain(self.0))
}
}
};
}
14 changes: 8 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ macro_rules! bitflags {
unused_attributes,
unused_mut,
unused_imports,
non_upper_case_globals
non_upper_case_globals,
clippy::assign_op_pattern
)]
const _: () = {
// Declared in a "hidden" scope that can't be reached directly
Expand All @@ -610,7 +611,7 @@ macro_rules! bitflags {

// This is where new library trait implementations can be added
__impl_external_bitflags! {
InternalBitFlags: $T {
InternalBitFlags: $T, $BitFlags {
$(
$(#[$inner $($args)*])*
$Flag;
Expand All @@ -623,7 +624,7 @@ macro_rules! bitflags {
}

__impl_public_bitflags_iter! {
$BitFlags
$BitFlags: $T, $BitFlags
}
};

Expand Down Expand Up @@ -657,11 +658,12 @@ macro_rules! bitflags {
unused_attributes,
unused_mut,
unused_imports,
non_upper_case_globals
non_upper_case_globals,
clippy::assign_op_pattern
)]
const _: () = {
__impl_public_bitflags! {
$BitFlags: $T {
$BitFlags: $T, $BitFlags {
$(
$(#[$inner $($args)*])*
$Flag;
Expand All @@ -670,7 +672,7 @@ macro_rules! bitflags {
}

__impl_public_bitflags_iter! {
$BitFlags
$BitFlags: $T, $BitFlags
}
};

Expand Down

0 comments on commit 32d406a

Please sign in to comment.