Skip to content

Commit d6320bd

Browse files
committed
Mark trivial functions with #[inline(always)]
1 parent ec6f62b commit d6320bd

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

enumflags_derive/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@ fn gen_enumflags(ast: &ItemEnum)
226226
#(#deferred)*
227227
impl #std_path::ops::Not for #ident {
228228
type Output = ::enumflags2::BitFlags<#ident>;
229+
#[inline(always)]
229230
fn not(self) -> Self::Output {
230231
use ::enumflags2::{BitFlags, _internal::RawBitFlags};
231232
unsafe { BitFlags::from_bits_unchecked(self.bits()).not() }
@@ -234,6 +235,7 @@ fn gen_enumflags(ast: &ItemEnum)
234235

235236
impl #std_path::ops::BitOr for #ident {
236237
type Output = ::enumflags2::BitFlags<#ident>;
238+
#[inline(always)]
237239
fn bitor(self, other: Self) -> Self::Output {
238240
use ::enumflags2::{BitFlags, _internal::RawBitFlags};
239241
unsafe { BitFlags::from_bits_unchecked(self.bits() | other.bits())}
@@ -242,6 +244,7 @@ fn gen_enumflags(ast: &ItemEnum)
242244

243245
impl #std_path::ops::BitAnd for #ident {
244246
type Output = ::enumflags2::BitFlags<#ident>;
247+
#[inline(always)]
245248
fn bitand(self, other: Self) -> Self::Output {
246249
use ::enumflags2::{BitFlags, _internal::RawBitFlags};
247250
unsafe { BitFlags::from_bits_unchecked(self.bits() & other.bits())}
@@ -250,6 +253,7 @@ fn gen_enumflags(ast: &ItemEnum)
250253

251254
impl #std_path::ops::BitXor for #ident {
252255
type Output = ::enumflags2::BitFlags<#ident>;
256+
#[inline(always)]
253257
fn bitxor(self, other: Self) -> Self::Output {
254258
#std_path::convert::Into::<Self::Output>::into(self) ^ #std_path::convert::Into::<Self::Output>::into(other)
255259
}

src/lib.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,14 @@ impl<T> Default for BitFlags<T>
352352
where
353353
T: BitFlag,
354354
{
355+
#[inline(always)]
355356
fn default() -> Self {
356357
Self::empty()
357358
}
358359
}
359360

360361
impl<T: BitFlag> From<T> for BitFlags<T> {
362+
#[inline(always)]
361363
fn from(t: T) -> BitFlags<T> {
362364
Self::from_flag(t)
363365
}
@@ -376,6 +378,7 @@ where
376378
///
377379
/// The argument must not have set bits at positions not corresponding to
378380
/// any flag.
381+
#[inline(always)]
379382
pub unsafe fn from_bits_unchecked(val: T::Numeric) -> Self {
380383
BitFlags { val, marker: PhantomData }
381384
}
@@ -403,8 +406,9 @@ where
403406
/// assert_eq!(empty.contains(MyFlag::Two), false);
404407
/// assert_eq!(empty.contains(MyFlag::Three), false);
405408
/// ```
409+
#[inline(always)]
406410
pub fn empty() -> Self {
407-
unsafe { BitFlags::from_bits_unchecked(T::Numeric::default()) }
411+
Self::EMPTY
408412
}
409413

410414
/// Create a `BitFlags` with all flags set.
@@ -430,8 +434,9 @@ where
430434
/// assert_eq!(empty.contains(MyFlag::Two), true);
431435
/// assert_eq!(empty.contains(MyFlag::Three), true);
432436
/// ```
437+
#[inline(always)]
433438
pub fn all() -> Self {
434-
unsafe { BitFlags::from_bits_unchecked(T::ALL_BITS) }
439+
Self::ALL
435440
}
436441

437442
/// An empty `BitFlags`. Equivalent to [`empty()`],
@@ -447,26 +452,45 @@ where
447452
pub const ALL: Self = BitFlags { val: T::ALL_BITS, marker: PhantomData };
448453

449454
/// Returns true if all flags are set
455+
#[inline(always)]
450456
pub fn is_all(self) -> bool {
451457
self.val == T::ALL_BITS
452458
}
453459

454460
/// Returns true if no flag is set
461+
#[inline(always)]
455462
pub fn is_empty(self) -> bool {
456-
self.val == Self::empty().bits()
463+
self.val == T::EMPTY
457464
}
458465

459-
/// Returns the underlying type value
466+
/// Returns the underlying bitwise value.
467+
///
468+
/// ```
469+
/// # use enumflags2::{bitflags, BitFlags};
470+
/// #[bitflags]
471+
/// #[repr(u8)]
472+
/// #[derive(Clone, Copy)]
473+
/// enum Flags {
474+
/// Foo = 1 << 0,
475+
/// Bar = 1 << 1,
476+
/// }
477+
///
478+
/// let both_flags = Flags::Foo | Flags::Bar;
479+
/// assert_eq!(both_flags.bits(), 0b11);
480+
/// ```
481+
#[inline(always)]
460482
pub fn bits(self) -> T::Numeric {
461483
self.val
462484
}
463485

464486
/// Returns true if at least one flag is shared.
487+
#[inline(always)]
465488
pub fn intersects<B: Into<BitFlags<T>>>(self, other: B) -> bool {
466-
(self.bits() & other.into().bits()) > Self::empty().bits()
489+
(self.bits() & other.into().bits()) != Self::EMPTY.val
467490
}
468491

469492
/// Returns true if all flags are contained.
493+
#[inline(always)]
470494
pub fn contains<B: Into<BitFlags<T>>>(self, other: B) -> bool {
471495
let other = other.into();
472496
(self.bits() & other.bits()) == other.bits()
@@ -487,26 +511,31 @@ where
487511
}
488512

489513
/// Turn a `T` into a `BitFlags<T>`. Also available as `flag.into()`.
514+
#[inline(always)]
490515
pub fn from_flag(flag: T) -> Self {
491516
unsafe { Self::from_bits_unchecked(flag.bits()) }
492517
}
493518

494519
/// Truncates flags that are illegal
520+
#[inline(always)]
495521
pub fn from_bits_truncate(bits: T::Numeric) -> Self {
496522
unsafe { BitFlags::from_bits_unchecked(bits & T::ALL_BITS) }
497523
}
498524

499525
/// Toggles the matching bits
526+
#[inline(always)]
500527
pub fn toggle<B: Into<BitFlags<T>>>(&mut self, other: B) {
501528
*self ^= other.into();
502529
}
503530

504531
/// Inserts the flags into the BitFlag
532+
#[inline(always)]
505533
pub fn insert<B: Into<BitFlags<T>>>(&mut self, other: B) {
506534
*self |= other.into();
507535
}
508536

509537
/// Removes the matching flags
538+
#[inline(always)]
510539
pub fn remove<B: Into<BitFlags<T>>>(&mut self, other: B) {
511540
*self &= !other.into();
512541
}
@@ -522,6 +551,7 @@ where
522551
T: BitFlag,
523552
B: Into<BitFlags<T>> + Copy,
524553
{
554+
#[inline(always)]
525555
fn eq(&self, other: &B) -> bool {
526556
self.bits() == Into::<Self>::into(*other).bits()
527557
}
@@ -533,6 +563,7 @@ where
533563
B: Into<BitFlags<T>>,
534564
{
535565
type Output = BitFlags<T>;
566+
#[inline(always)]
536567
fn bitor(self, other: B) -> BitFlags<T> {
537568
unsafe { BitFlags::from_bits_unchecked(self.bits() | other.into().bits()) }
538569
}
@@ -544,6 +575,7 @@ where
544575
B: Into<BitFlags<T>>,
545576
{
546577
type Output = BitFlags<T>;
578+
#[inline(always)]
547579
fn bitand(self, other: B) -> BitFlags<T> {
548580
unsafe { BitFlags::from_bits_unchecked(self.bits() & other.into().bits()) }
549581
}
@@ -555,6 +587,7 @@ where
555587
B: Into<BitFlags<T>>,
556588
{
557589
type Output = BitFlags<T>;
590+
#[inline(always)]
558591
fn bitxor(self, other: B) -> BitFlags<T> {
559592
unsafe { BitFlags::from_bits_unchecked(self.bits() ^ other.into().bits()) }
560593
}
@@ -565,6 +598,7 @@ where
565598
T: BitFlag,
566599
B: Into<BitFlags<T>>,
567600
{
601+
#[inline(always)]
568602
fn bitor_assign(&mut self, other: B) {
569603
*self = *self | other;
570604
}
@@ -575,6 +609,7 @@ where
575609
T: BitFlag,
576610
B: Into<BitFlags<T>>,
577611
{
612+
#[inline(always)]
578613
fn bitand_assign(&mut self, other: B) {
579614
*self = *self & other;
580615
}
@@ -584,6 +619,7 @@ where
584619
T: BitFlag,
585620
B: Into<BitFlags<T>>,
586621
{
622+
#[inline(always)]
587623
fn bitxor_assign(&mut self, other: B) {
588624
*self = *self ^ other;
589625
}
@@ -594,6 +630,7 @@ where
594630
T: BitFlag,
595631
{
596632
type Output = BitFlags<T>;
633+
#[inline(always)]
597634
fn not(self) -> BitFlags<T> {
598635
unsafe { BitFlags::from_bits_unchecked(!self.bits() & T::ALL_BITS) }
599636
}

0 commit comments

Comments
 (0)