forked from bitflags/bitflags
/
bitflags_trait.rs
108 lines (96 loc) · 3.05 KB
/
bitflags_trait.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not};
#[doc(hidden)]
pub trait ImplementedByBitFlagsMacro {}
/// A trait that is automatically implemented for all bitflags.
///
/// It should not be implemented manually.
pub trait BitFlags: ImplementedByBitFlagsMacro {
type Bits: Bits;
/// Returns an empty set of flags.
fn empty() -> Self;
/// Returns the set containing all flags.
fn all() -> Self;
/// Returns the raw value of the flags currently stored.
fn bits(&self) -> Self::Bits;
/// Convert from underlying bit representation, unless that
/// representation contains bits that do not correspond to a flag.
fn from_bits(bits: Self::Bits) -> Option<Self>
where
Self: Sized;
/// Convert from underlying bit representation, dropping any bits
/// that do not correspond to flags.
fn from_bits_truncate(bits: Self::Bits) -> Self;
/// Convert from underlying bit representation, preserving all
/// bits (even those not corresponding to a defined flag).
fn from_bits_retain(bits: Self::Bits) -> Self;
/// Returns `true` if no flags are currently stored.
fn is_empty(&self) -> bool;
/// Returns `true` if all flags are currently set.
fn is_all(&self) -> bool;
/// Returns `true` if there are flags common to both `self` and `other`.
fn intersects(&self, other: Self) -> bool;
/// Returns `true` all of the flags in `other` are contained within `self`.
fn contains(&self, other: Self) -> bool;
/// Inserts the specified flags in-place.
fn insert(&mut self, other: Self);
/// Removes the specified flags in-place.
fn remove(&mut self, other: Self);
/// Toggles the specified flags in-place.
fn toggle(&mut self, other: Self);
/// Inserts or removes the specified flags depending on the passed value.
fn set(&mut self, other: Self, value: bool);
}
// Not re-exported
pub trait Sealed {}
/// A private trait that encodes the requirements of underlying bits types that can hold flags.
///
/// This trait may be made public at some future point, but it presents a compatibility hazard
/// so is left internal for now.
#[doc(hidden)]
pub trait Bits:
Clone
+ Copy
+ BitAnd
+ BitAndAssign
+ BitOr
+ BitOrAssign
+ BitXor
+ BitXorAssign
+ Not
+ Sized
+ Sealed
{
/// The value of `Self` where no bits are set.
const EMPTY: Self;
/// The value of `Self` where all bits are set.
const ALL: Self;
}
macro_rules! impl_bits {
($($u:ty, $i:ty,)*) => {
$(
impl Bits for $u {
const EMPTY: $u = 0;
const ALL: $u = <$u>::MAX;
}
impl Bits for $i {
const EMPTY: $i = 0;
const ALL: $i = <$u>::MAX as $i;
}
impl Sealed for $u {}
impl Sealed for $i {}
)*
}
}
impl_bits! {
u8, i8,
u16, i16,
u32, i32,
u64, i64,
u128, i128,
}
pub trait PublicFlags {
type InternalFlags;
}
pub trait InternalFlags {
type PublicFlags;
}