aboutsummaryrefslogtreecommitdiff
path: root/src/public.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/public.rs')
-rw-r--r--src/public.rs575
1 files changed, 293 insertions, 282 deletions
diff --git a/src/public.rs b/src/public.rs
index 643f843..57ab0ea 100644
--- a/src/public.rs
+++ b/src/public.rs
@@ -11,10 +11,10 @@
macro_rules! __declare_public_bitflags {
(
$(#[$outer:meta])*
- $vis:vis struct $BitFlags:ident;
+ $vis:vis struct $PublicBitFlags:ident
) => {
$(#[$outer])*
- $vis struct $BitFlags(<$BitFlags as $crate::__private::PublicFlags>::Internal);
+ $vis struct $PublicBitFlags(<$PublicBitFlags as $crate::__private::PublicFlags>::Internal);
};
}
@@ -24,236 +24,302 @@ macro_rules! __declare_public_bitflags {
/// could conflict with items added by the end-user.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
-macro_rules! __impl_public_bitflags {
+macro_rules! __impl_public_bitflags_forward {
(
- $PublicBitFlags:ident: $T:ty, $InternalBitFlags:ident, $Iter:ident, $IterNames:ident;
+ $PublicBitFlags:ident: $T:ty, $InternalBitFlags:ident
) => {
- impl $crate::__private::core::fmt::Binary for $PublicBitFlags {
- fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
- $crate::__private::core::fmt::Binary::fmt(&self.0, f)
- }
- }
+ __impl_bitflags! {
+ $PublicBitFlags: $T {
+ fn empty() {
+ Self($InternalBitFlags::empty())
+ }
- impl $crate::__private::core::fmt::Octal for $PublicBitFlags {
- fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
- $crate::__private::core::fmt::Octal::fmt(&self.0, f)
- }
- }
+ fn all() {
+ Self($InternalBitFlags::all())
+ }
- impl $crate::__private::core::fmt::LowerHex for $PublicBitFlags {
- fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
- $crate::__private::core::fmt::LowerHex::fmt(&self.0, f)
+ fn bits(f) {
+ f.0.bits()
+ }
+
+ fn from_bits(bits) {
+ match $InternalBitFlags::from_bits(bits) {
+ $crate::__private::core::option::Option::Some(bits) => $crate::__private::core::option::Option::Some(Self(bits)),
+ $crate::__private::core::option::Option::None => $crate::__private::core::option::Option::None,
+ }
+ }
+
+ fn from_bits_truncate(bits) {
+ Self($InternalBitFlags::from_bits_truncate(bits))
+ }
+
+ fn from_bits_retain(bits) {
+ Self($InternalBitFlags::from_bits_retain(bits))
+ }
+
+ fn from_name(name) {
+ match $InternalBitFlags::from_name(name) {
+ $crate::__private::core::option::Option::Some(bits) => $crate::__private::core::option::Option::Some(Self(bits)),
+ $crate::__private::core::option::Option::None => $crate::__private::core::option::Option::None,
+ }
+ }
+
+ fn is_empty(f) {
+ f.0.is_empty()
+ }
+
+ fn is_all(f) {
+ f.0.is_all()
+ }
+
+ fn intersects(f, other) {
+ f.0.intersects(other.0)
+ }
+
+ fn contains(f, other) {
+ f.0.contains(other.0)
+ }
+
+ fn insert(f, other) {
+ f.0.insert(other.0)
+ }
+
+ fn remove(f, other) {
+ f.0.remove(other.0)
+ }
+
+ fn toggle(f, other) {
+ f.0.toggle(other.0)
+ }
+
+ fn set(f, other, value) {
+ f.0.set(other.0, value)
+ }
+
+ fn intersection(f, other) {
+ Self(f.0.intersection(other.0))
+ }
+
+ fn union(f, other) {
+ Self(f.0.union(other.0))
+ }
+
+ fn difference(f, other) {
+ Self(f.0.difference(other.0))
+ }
+
+ fn symmetric_difference(f, other) {
+ Self(f.0.symmetric_difference(other.0))
+ }
+
+ fn complement(f) {
+ Self(f.0.complement())
+ }
}
}
- impl $crate::__private::core::fmt::UpperHex for $PublicBitFlags {
- fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
- $crate::__private::core::fmt::UpperHex::fmt(&self.0, f)
- }
+ __impl_public_bitflags_ops!($PublicBitFlags);
+ };
+}
+
+/// Implement functions on the public (user-facing) bitflags type.
+///
+/// We need to be careful about adding new methods and trait implementations here because they
+/// could conflict with items added by the end-user.
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! __impl_public_bitflags {
+ (
+ $BitFlags:ident: $T:ty, $PublicBitFlags:ident {
+ $(
+ $(#[$attr:ident $($args:tt)*])*
+ $Flag:ident;
+ )*
}
+ ) => {
+ __impl_bitflags! {
+ $BitFlags: $T {
+ fn empty() {
+ Self(<$T as $crate::Bits>::EMPTY)
+ }
- impl $PublicBitFlags {
- /// Returns an empty set of flags.
- #[inline]
- pub const fn empty() -> Self {
- Self($InternalBitFlags::empty())
- }
+ fn all() {
+ Self::from_bits_truncate(<$T as $crate::Bits>::ALL)
+ }
- /// Returns the set containing all flags.
- #[inline]
- pub const fn all() -> Self {
- Self($InternalBitFlags::all())
- }
+ fn bits(f) {
+ f.0
+ }
- /// Returns the raw value of the flags currently stored.
- #[inline]
- pub const fn bits(&self) -> $T {
- self.0.bits()
- }
+ fn from_bits(bits) {
+ let truncated = Self::from_bits_truncate(bits).0;
- /// Convert from underlying bit representation, unless that
- /// representation contains bits that do not correspond to a flag.
- #[inline]
- pub const fn from_bits(bits: $T) -> $crate::__private::core::option::Option<Self> {
- match $InternalBitFlags::from_bits(bits) {
- $crate::__private::core::option::Option::Some(bits) => $crate::__private::core::option::Option::Some(Self(bits)),
- $crate::__private::core::option::Option::None => $crate::__private::core::option::Option::None,
+ if truncated == bits {
+ $crate::__private::core::option::Option::Some(Self(bits))
+ } else {
+ $crate::__private::core::option::Option::None
+ }
}
- }
- /// Convert from underlying bit representation, dropping any bits
- /// that do not correspond to flags.
- #[inline]
- pub const fn from_bits_truncate(bits: $T) -> Self {
- Self($InternalBitFlags::from_bits_truncate(bits))
- }
+ fn from_bits_truncate(bits) {
+ if bits == <$T as $crate::Bits>::EMPTY {
+ return Self(bits)
+ }
+
+ let mut truncated = <$T as $crate::Bits>::EMPTY;
+
+ $(
+ __bitflags_expr_safe_attrs!(
+ $(#[$attr $($args)*])*
+ {
+ if bits & $PublicBitFlags::$Flag.bits() == $PublicBitFlags::$Flag.bits() {
+ truncated = truncated | $PublicBitFlags::$Flag.bits()
+ }
+ }
+ );
+ )*
+
+ Self(truncated)
+ }
- /// Convert from underlying bit representation, preserving all
- /// bits (even those not corresponding to a defined flag).
- #[inline]
- pub const fn from_bits_retain(bits: $T) -> Self {
- Self($InternalBitFlags::from_bits_retain(bits))
- }
+ fn from_bits_retain(bits) {
+ Self(bits)
+ }
- /// Get the value for a flag from its stringified name.
- ///
- /// Names are _case-sensitive_, so must correspond exactly to
- /// the identifier given to the flag.
- #[inline]
- pub fn from_name(name: &str) -> $crate::__private::core::option::Option<Self> {
- match $InternalBitFlags::from_name(name) {
- $crate::__private::core::option::Option::Some(bits) => $crate::__private::core::option::Option::Some(Self(bits)),
- $crate::__private::core::option::Option::None => $crate::__private::core::option::Option::None,
+ fn from_name(name) {
+ $(
+ __bitflags_expr_safe_attrs!(
+ $(#[$attr $($args)*])*
+ {
+ if name == $crate::__private::core::stringify!($Flag) {
+ return $crate::__private::core::option::Option::Some(Self($PublicBitFlags::$Flag.bits()));
+ }
+ }
+ );
+ )*
+
+ let _ = name;
+ $crate::__private::core::option::Option::None
}
- }
- /// Iterate over enabled flag values.
- #[inline]
- pub const fn iter(&self) -> $Iter {
- self.0.iter()
- }
+ fn is_empty(f) {
+ f.0 == Self::empty().0
+ }
- /// Iterate over enabled flag values with their stringified names.
- #[inline]
- pub const fn iter_names(&self) -> $IterNames {
- self.0.iter_names()
- }
+ fn is_all(f) {
+ Self::all().0 | f.0 == f.0
+ }
- /// Returns `true` if no flags are currently stored.
- #[inline]
- pub const fn is_empty(&self) -> bool {
- self.0.is_empty()
- }
+ fn intersects(f, other) {
+ !(Self(f.0 & other.0)).is_empty()
+ }
- /// Returns `true` if all flags are currently set.
- #[inline]
- pub const fn is_all(&self) -> bool {
- self.0.is_all()
- }
+ fn contains(f, other) {
+ (f.0 & other.0) == other.0
+ }
- /// Returns `true` if there are flags common to both `self` and `other`.
- #[inline]
- pub const fn intersects(&self, other: Self) -> bool {
- self.0.intersects(other.0)
- }
+ fn insert(f, other) {
+ f.0 = f.0 | other.0;
+ }
- /// Returns `true` if all of the flags in `other` are contained within `self`.
- #[inline]
- pub const fn contains(&self, other: Self) -> bool {
- self.0.contains(other.0)
- }
+ fn remove(f, other) {
+ f.0 = f.0 & !other.0;
+ }
- /// Inserts the specified flags in-place.
- #[inline]
- pub fn insert(&mut self, other: Self) {
- self.0.insert(other.0)
- }
+ fn toggle(f, other) {
+ f.0 = f.0 ^ other.0;
+ }
- /// Removes the specified flags in-place.
- #[inline]
- pub fn remove(&mut self, other: Self) {
- self.0.remove(other.0)
+ fn set(f, other, value) {
+ if value {
+ f.insert(other);
+ } else {
+ f.remove(other);
+ }
+ }
+
+ fn intersection(f, other) {
+ Self(f.0 & other.0)
+ }
+
+ fn union(f, other) {
+ Self(f.0 | other.0)
+ }
+
+ fn difference(f, other) {
+ Self(f.0 & !other.0)
+ }
+
+ fn symmetric_difference(f, other) {
+ Self(f.0 ^ other.0)
+ }
+
+ fn complement(f) {
+ Self::from_bits_truncate(!f.0)
+ }
}
+ }
+
+ __impl_public_bitflags_ops!($BitFlags);
+ };
+}
- /// Toggles the specified flags in-place.
+/// Implement iterators on the public (user-facing) bitflags type.
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! __impl_public_bitflags_iter {
+ ($BitFlags:ident: $T:ty, $PublicBitFlags:ident) => {
+ impl $BitFlags {
+ /// Iterate over enabled flag values.
#[inline]
- pub fn toggle(&mut self, other: Self) {
- self.0.toggle(other.0)
+ 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.bits()), $PublicBitFlags::from_bits_retain(self.bits()))
}
- /// Inserts or removes the specified flags depending on the passed value.
+ /// Iterate over enabled flag values with their stringified names.
#[inline]
- pub fn set(&mut self, other: Self, value: bool) {
- self.0.set(other.0, value)
+ 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.bits()), $PublicBitFlags::from_bits_retain(self.bits()))
}
+ }
- /// Returns the intersection between the flags in `self` and
- /// `other`.
- ///
- /// Specifically, the returned set contains only the flags which are
- /// present in *both* `self` *and* `other`.
- ///
- /// This is equivalent to using the `&` operator (e.g.
- /// [`ops::BitAnd`]), as in `flags & other`.
- ///
- /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
- #[inline]
- #[must_use]
- pub const fn intersection(self, other: Self) -> Self {
- Self(self.0.intersection(other.0))
+ impl $crate::__private::core::iter::IntoIterator for $BitFlags {
+ type Item = $PublicBitFlags;
+ type IntoIter = $crate::iter::Iter<$PublicBitFlags>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.iter()
}
+ }
+ };
+}
- /// Returns the union of between the flags in `self` and `other`.
- ///
- /// Specifically, the returned set contains all flags which are
- /// present in *either* `self` *or* `other`, including any which are
- /// present in both (see [`Self::symmetric_difference`] if that
- /// is undesirable).
- ///
- /// This is equivalent to using the `|` operator (e.g.
- /// [`ops::BitOr`]), as in `flags | other`.
- ///
- /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
- #[inline]
- #[must_use]
- pub const fn union(self, other: Self) -> Self {
- Self(self.0.union(other.0))
+/// Implement traits on the public (user-facing) bitflags type.
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! __impl_public_bitflags_ops {
+ ($PublicBitFlags:ident) => {
+ impl $crate::__private::core::fmt::Binary for $PublicBitFlags {
+ fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
+ $crate::__private::core::fmt::Binary::fmt(&self.0, f)
}
+ }
- /// Returns the difference between the flags in `self` and `other`.
- ///
- /// Specifically, the returned set contains all flags present in
- /// `self`, except for the ones present in `other`.
- ///
- /// It is also conceptually equivalent to the "bit-clear" operation:
- /// `flags & !other` (and this syntax is also supported).
- ///
- /// This is equivalent to using the `-` operator (e.g.
- /// [`ops::Sub`]), as in `flags - other`.
- ///
- /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
- #[inline]
- #[must_use]
- pub const fn difference(self, other: Self) -> Self {
- Self(self.0.difference(other.0))
+ impl $crate::__private::core::fmt::Octal for $PublicBitFlags {
+ fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
+ $crate::__private::core::fmt::Octal::fmt(&self.0, f)
}
+ }
- /// Returns the [symmetric difference][sym-diff] between the flags
- /// in `self` and `other`.
- ///
- /// Specifically, the returned set contains the flags present which
- /// are present in `self` or `other`, but that are not present in
- /// both. Equivalently, it contains the flags present in *exactly
- /// one* of the sets `self` and `other`.
- ///
- /// This is equivalent to using the `^` operator (e.g.
- /// [`ops::BitXor`]), as in `flags ^ other`.
- ///
- /// [sym-diff]: https://en.wikipedia.org/wiki/Symmetric_difference
- /// [`ops::BitXor`]: https://doc.rust-lang.org/std/ops/trait.BitXor.html
- #[inline]
- #[must_use]
- pub const fn symmetric_difference(self, other: Self) -> Self {
- Self(self.0.symmetric_difference(other.0))
+ impl $crate::__private::core::fmt::LowerHex for $PublicBitFlags {
+ fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
+ $crate::__private::core::fmt::LowerHex::fmt(&self.0, f)
}
+ }
- /// Returns the complement of this set of flags.
- ///
- /// Specifically, the returned set contains all the flags which are
- /// not set in `self`, but which are allowed for this type.
- ///
- /// Alternatively, it can be thought of as the set difference
- /// between [`Self::all()`] and `self` (e.g. `Self::all() - self`)
- ///
- /// This is equivalent to using the `!` operator (e.g.
- /// [`ops::Not`]), as in `!flags`.
- ///
- /// [`Self::all()`]: Self::all
- /// [`ops::Not`]: https://doc.rust-lang.org/std/ops/trait.Not.html
- #[inline]
- #[must_use]
- pub const fn complement(self) -> Self {
- Self(self.0.complement())
+ impl $crate::__private::core::fmt::UpperHex for $PublicBitFlags {
+ fn fmt(&self, f: &mut $crate::__private::core::fmt::Formatter) -> $crate::__private::core::fmt::Result {
+ $crate::__private::core::fmt::UpperHex::fmt(&self.0, f)
}
}
@@ -271,7 +337,7 @@ macro_rules! __impl_public_bitflags {
/// Adds the set of flags.
#[inline]
fn bitor_assign(&mut self, other: Self) {
- self.0 = self.0.union(other.0);
+ self.0 = self.0 | other.0;
}
}
@@ -289,7 +355,7 @@ macro_rules! __impl_public_bitflags {
/// Toggles the set of flags.
#[inline]
fn bitxor_assign(&mut self, other: Self) {
- self.0 = self.0.symmetric_difference(other.0);
+ self.0 = self.0 ^ other.0
}
}
@@ -307,7 +373,7 @@ macro_rules! __impl_public_bitflags {
/// Disables all flags disabled in the set.
#[inline]
fn bitand_assign(&mut self, other: Self) {
- self.0 = self.0.intersection(other.0);
+ self.0 = self.0 & other.0;
}
}
@@ -325,7 +391,7 @@ macro_rules! __impl_public_bitflags {
/// Disables all flags enabled in the set.
#[inline]
fn sub_assign(&mut self, other: Self) {
- self.0 = self.0.difference(other.0);
+ self.0 = self.0 & !other.0;
}
}
@@ -356,92 +422,6 @@ macro_rules! __impl_public_bitflags {
result
}
}
-
- impl $crate::__private::core::iter::IntoIterator for $PublicBitFlags {
- type Item = Self;
- type IntoIter = $Iter;
-
- fn into_iter(self) -> Self::IntoIter {
- self.0.iter()
- }
- }
-
- impl $crate::BitFlags for $PublicBitFlags {
- type Bits = $T;
-
- type Iter = $Iter;
- type IterNames = $IterNames;
-
- fn empty() -> Self {
- $PublicBitFlags::empty()
- }
-
- fn all() -> Self {
- $PublicBitFlags::all()
- }
-
- fn bits(&self) -> $T {
- $PublicBitFlags::bits(self)
- }
-
- fn from_bits(bits: $T) -> $crate::__private::core::option::Option<$PublicBitFlags> {
- $PublicBitFlags::from_bits(bits)
- }
-
- fn from_bits_truncate(bits: $T) -> $PublicBitFlags {
- $PublicBitFlags::from_bits_truncate(bits)
- }
-
- fn from_bits_retain(bits: $T) -> $PublicBitFlags {
- $PublicBitFlags::from_bits_retain(bits)
- }
-
- fn from_name(name: &str) -> $crate::__private::core::option::Option<$PublicBitFlags> {
- $PublicBitFlags::from_name(name)
- }
-
- fn iter(&self) -> Self::Iter {
- $PublicBitFlags::iter(self)
- }
-
- fn iter_names(&self) -> Self::IterNames {
- $PublicBitFlags::iter_names(self)
- }
-
- fn is_empty(&self) -> bool {
- $PublicBitFlags::is_empty(self)
- }
-
- fn is_all(&self) -> bool {
- $PublicBitFlags::is_all(self)
- }
-
- fn intersects(&self, other: $PublicBitFlags) -> bool {
- $PublicBitFlags::intersects(self, other)
- }
-
- fn contains(&self, other: $PublicBitFlags) -> bool {
- $PublicBitFlags::contains(self, other)
- }
-
- fn insert(&mut self, other: $PublicBitFlags) {
- $PublicBitFlags::insert(self, other)
- }
-
- fn remove(&mut self, other: $PublicBitFlags) {
- $PublicBitFlags::remove(self, other)
- }
-
- fn toggle(&mut self, other: $PublicBitFlags) {
- $PublicBitFlags::toggle(self, other)
- }
-
- fn set(&mut self, other: $PublicBitFlags, value: bool) {
- $PublicBitFlags::set(self, other, value)
- }
- }
-
- impl $crate::__private::ImplementedByBitFlagsMacro for $PublicBitFlags {}
};
}
@@ -450,7 +430,7 @@ macro_rules! __impl_public_bitflags {
#[doc(hidden)]
macro_rules! __impl_public_bitflags_consts {
(
- $PublicBitFlags:ident {
+ $PublicBitFlags:ident: $T:ty {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident = $value:expr;
@@ -460,8 +440,39 @@ macro_rules! __impl_public_bitflags_consts {
impl $PublicBitFlags {
$(
$(#[$attr $($args)*])*
+ #[allow(
+ deprecated,
+ non_upper_case_globals,
+ )]
pub const $Flag: Self = Self::from_bits_retain($value);
)*
}
+
+ impl $crate::Flags for $PublicBitFlags {
+ const FLAGS: &'static [$crate::Flag<$PublicBitFlags>] = &[
+ $(
+ __bitflags_expr_safe_attrs!(
+ $(#[$attr $($args)*])*
+ {
+ #[allow(
+ deprecated,
+ non_upper_case_globals,
+ )]
+ $crate::Flag::new($crate::__private::core::stringify!($Flag), $PublicBitFlags::$Flag)
+ }
+ ),
+ )*
+ ];
+
+ type Bits = $T;
+
+ fn bits(&self) -> $T {
+ $PublicBitFlags::bits(self)
+ }
+
+ fn from_bits_retain(bits: $T) -> $PublicBitFlags {
+ $PublicBitFlags::from_bits_retain(bits)
+ }
+ }
};
}