Skip to content

Commit 9e1cf3e

Browse files
authored
Merge pull request #458 from nnethercote/streamline-generated-code
Streamline generated code
2 parents 85b5e60 + a3f1f78 commit 9e1cf3e

File tree

2 files changed

+128
-170
lines changed

2 files changed

+128
-170
lines changed

src/lib.rs

Lines changed: 67 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -604,199 +604,155 @@ macro_rules! bitflags {
604604
#[doc(hidden)]
605605
macro_rules! __impl_bitflags {
606606
(
607+
// These param names must be passed in to make the macro work.
608+
// Just use `params: self, bits, name, other, value;`.
609+
params: $self:ident, $bits:ident, $name:ident, $other:ident, $value:ident;
607610
$(#[$outer:meta])*
608611
$PublicBitFlags:ident: $T:ty {
609-
fn empty() $empty:block
610-
fn all() $all:block
611-
fn bits($bits0:ident) $bits:block
612-
fn from_bits($from_bits0:ident) $from_bits:block
613-
fn from_bits_truncate($from_bits_truncate0:ident) $from_bits_truncate:block
614-
fn from_bits_retain($from_bits_retain0:ident) $from_bits_retain:block
615-
fn from_name($from_name0:ident) $from_name:block
616-
fn is_empty($is_empty0:ident) $is_empty:block
617-
fn is_all($is_all0:ident) $is_all:block
618-
fn intersects($intersects0:ident, $intersects1:ident) $intersects:block
619-
fn contains($contains0:ident, $contains1:ident) $contains:block
620-
fn insert($insert0:ident, $insert1:ident) $insert:block
621-
fn remove($remove0:ident, $remove1:ident) $remove:block
622-
fn toggle($toggle0:ident, $toggle1:ident) $toggle:block
623-
fn set($set0:ident, $set1:ident, $set2:ident) $set:block
624-
fn intersection($intersection0:ident, $intersection1:ident) $intersection:block
625-
fn union($union0:ident, $union1:ident) $union:block
626-
fn difference($difference0:ident, $difference1:ident) $difference:block
627-
fn symmetric_difference($symmetric_difference0:ident, $symmetric_difference1:ident) $symmetric_difference:block
628-
fn complement($complement0:ident) $complement:block
612+
fn empty() $empty_body:block
613+
fn all() $all_body:block
614+
fn bits(&self) $bits_body:block
615+
fn from_bits(bits) $from_bits_body:block
616+
fn from_bits_truncate(bits) $from_bits_truncate_body:block
617+
fn from_bits_retain(bits) $from_bits_retain_body:block
618+
fn from_name(name) $from_name_body:block
619+
fn is_empty(&self) $is_empty_body:block
620+
fn is_all(&self) $is_all_body:block
621+
fn intersects(&self, other) $intersects_body:block
622+
fn contains(&self, other) $contains_body:block
623+
fn insert(&mut self, other) $insert_body:block
624+
fn remove(&mut self, other) $remove_body:block
625+
fn toggle(&mut self, other) $toggle_body:block
626+
fn set(&mut self, other, value) $set_body:block
627+
fn intersection(self, other) $intersection_body:block
628+
fn union(self, other) $union_body:block
629+
fn difference(self, other) $difference_body:block
630+
fn symmetric_difference(self, other) $symmetric_difference_body:block
631+
fn complement(self) $complement_body:block
629632
}
630633
) => {
631634
#[allow(dead_code, deprecated, unused_attributes)]
632635
$(#[$outer])*
633636
impl $PublicBitFlags {
634637
/// Get a flags value with all bits unset.
635638
#[inline]
636-
pub const fn empty() -> Self {
637-
$empty
638-
}
639+
pub const fn empty() -> Self
640+
$empty_body
639641

640642
/// Get a flags value with all known bits set.
641643
#[inline]
642-
pub const fn all() -> Self {
643-
$all
644-
}
644+
pub const fn all() -> Self
645+
$all_body
645646

646647
/// Get the underlying bits value.
647648
///
648649
/// The returned value is exactly the bits set in this flags value.
649650
#[inline]
650-
pub const fn bits(&self) -> $T {
651-
let $bits0 = self;
652-
$bits
653-
}
651+
pub const fn bits(&$self) -> $T
652+
$bits_body
654653

655654
/// Convert from a bits value.
656655
///
657656
/// This method will return `None` if any unknown bits are set.
658657
#[inline]
659-
pub const fn from_bits(bits: $T) -> $crate::__private::core::option::Option<Self> {
660-
let $from_bits0 = bits;
661-
$from_bits
662-
}
658+
pub const fn from_bits($bits: $T) -> $crate::__private::core::option::Option<Self>
659+
$from_bits_body
663660

664661
/// Convert from a bits value, unsetting any unknown bits.
665662
#[inline]
666-
pub const fn from_bits_truncate(bits: $T) -> Self {
667-
let $from_bits_truncate0 = bits;
668-
$from_bits_truncate
669-
}
663+
pub const fn from_bits_truncate($bits: $T) -> Self
664+
$from_bits_truncate_body
670665

671666
/// Convert from a bits value exactly.
672667
#[inline]
673-
pub const fn from_bits_retain(bits: $T) -> Self {
674-
let $from_bits_retain0 = bits;
675-
$from_bits_retain
676-
}
668+
pub const fn from_bits_retain($bits: $T) -> Self
669+
$from_bits_retain_body
677670

678671
/// Get a flags value with the bits of a flag with the given name set.
679672
///
680673
/// This method will return `None` if `name` is empty or doesn't
681674
/// correspond to any named flag.
682675
#[inline]
683-
pub fn from_name(name: &str) -> $crate::__private::core::option::Option<Self> {
684-
let $from_name0 = name;
685-
$from_name
686-
}
676+
pub fn from_name($name: &str) -> $crate::__private::core::option::Option<Self>
677+
$from_name_body
687678

688679
/// Whether all bits in this flags value are unset.
689680
#[inline]
690-
pub const fn is_empty(&self) -> bool {
691-
let $is_empty0 = self;
692-
$is_empty
693-
}
681+
pub const fn is_empty(&$self) -> bool
682+
$is_empty_body
694683

695684
/// Whether all known bits in this flags value are set.
696685
#[inline]
697-
pub const fn is_all(&self) -> bool {
698-
let $is_all0 = self;
699-
$is_all
700-
}
686+
pub const fn is_all(&$self) -> bool
687+
$is_all_body
701688

702689
/// Whether any set bits in a source flags value are also set in a target flags value.
703690
#[inline]
704-
pub const fn intersects(&self, other: Self) -> bool {
705-
let $intersects0 = self;
706-
let $intersects1 = other;
707-
$intersects
708-
}
691+
pub const fn intersects(&$self, $other: Self) -> bool
692+
$intersects_body
709693

710694
/// Whether all set bits in a source flags value are also set in a target flags value.
711695
#[inline]
712-
pub const fn contains(&self, other: Self) -> bool {
713-
let $contains0 = self;
714-
let $contains1 = other;
715-
$contains
716-
}
696+
pub const fn contains(&$self, $other: Self) -> bool
697+
$contains_body
717698

718699
/// The bitwise or (`|`) of the bits in two flags values.
719700
#[inline]
720-
pub fn insert(&mut self, other: Self) {
721-
let $insert0 = self;
722-
let $insert1 = other;
723-
$insert
724-
}
701+
pub fn insert(&mut $self, $other: Self)
702+
$insert_body
725703

726-
/// The intersection of a source flags value with the complement of a target flags value (`&!`).
704+
/// The intersection of a source flags value with the complement of a target flags
705+
/// value (`&!`).
727706
///
728707
/// This method is not equivalent to `self & !other` when `other` has unknown bits set.
729708
/// `remove` won't truncate `other`, but the `!` operator will.
730709
#[inline]
731-
pub fn remove(&mut self, other: Self) {
732-
let $remove0 = self;
733-
let $remove1 = other;
734-
$remove
735-
}
710+
pub fn remove(&mut $self, $other: Self)
711+
$remove_body
736712

737713
/// The bitwise exclusive-or (`^`) of the bits in two flags values.
738714
#[inline]
739-
pub fn toggle(&mut self, other: Self) {
740-
let $toggle0 = self;
741-
let $toggle1 = other;
742-
$toggle
743-
}
715+
pub fn toggle(&mut $self, $other: Self)
716+
$toggle_body
744717

745718
/// Call `insert` when `value` is `true` or `remove` when `value` is `false`.
746719
#[inline]
747-
pub fn set(&mut self, other: Self, value: bool) {
748-
let $set0 = self;
749-
let $set1 = other;
750-
let $set2 = value;
751-
$set
752-
}
720+
pub fn set(&mut $self, $other: Self, $value: bool)
721+
$set_body
753722

754723
/// The bitwise and (`&`) of the bits in two flags values.
755724
#[inline]
756725
#[must_use]
757-
pub const fn intersection(self, other: Self) -> Self {
758-
let $intersection0 = self;
759-
let $intersection1 = other;
760-
$intersection
761-
}
726+
pub const fn intersection($self, $other: Self) -> Self
727+
$intersection_body
762728

763729
/// The bitwise or (`|`) of the bits in two flags values.
764730
#[inline]
765731
#[must_use]
766-
pub const fn union(self, other: Self) -> Self {
767-
let $union0 = self;
768-
let $union1 = other;
769-
$union
770-
}
732+
pub const fn union($self, $other: Self) -> Self
733+
$union_body
771734

772-
/// The intersection of a source flags value with the complement of a target flags value (`&!`).
735+
/// The intersection of a source flags value with the complement of a target flags
736+
/// value (`&!`).
773737
///
774738
/// This method is not equivalent to `self & !other` when `other` has unknown bits set.
775739
/// `difference` won't truncate `other`, but the `!` operator will.
776740
#[inline]
777741
#[must_use]
778-
pub const fn difference(self, other: Self) -> Self {
779-
let $difference0 = self;
780-
let $difference1 = other;
781-
$difference
782-
}
742+
pub const fn difference($self, $other: Self) -> Self
743+
$difference_body
783744

784745
/// The bitwise exclusive-or (`^`) of the bits in two flags values.
785746
#[inline]
786747
#[must_use]
787-
pub const fn symmetric_difference(self, other: Self) -> Self {
788-
let $symmetric_difference0 = self;
789-
let $symmetric_difference1 = other;
790-
$symmetric_difference
791-
}
748+
pub const fn symmetric_difference($self, $other: Self) -> Self
749+
$symmetric_difference_body
792750

793751
/// The bitwise negation (`!`) of the bits in a flags value, truncating the result.
794752
#[inline]
795753
#[must_use]
796-
pub const fn complement(self) -> Self {
797-
let $complement0 = self;
798-
$complement
799-
}
754+
pub const fn complement($self) -> Self
755+
$complement_body
800756
}
801757
};
802758
}

0 commit comments

Comments
 (0)