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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::ops::{BitOr, BitAnd, BitXor, Not};
use std::cmp;

pub trait EnumFlagSize {
    type Size: InnerBitFlags;
}

pub trait InnerBitFlags: BitOr<Self> + cmp::PartialEq + cmp::Eq
    where Self: Sized
{
    type Type;
    fn all() -> Self;
    fn empty() -> Self;
    fn is_empty(self) -> bool;
    fn is_all(self) -> bool;
    fn bits(self) -> Self::Type;
    fn intersects(self, other: Self) -> bool;
    fn contains(self, other: Self) -> bool;
    fn not(self) -> Self;
    fn from_bits(bits: Self::Type) -> Option<Self>;
    fn from_bits_truncate(bits: Self::Type) -> Self;
    fn insert(&mut self, other: Self);
    fn remove(&mut self, other: Self);
    fn toggle(&mut self, other: Self);
}

#[derive(Eq, Copy, Clone)]
pub struct BitFlags<T: EnumFlagSize> {
    val: T::Size,
}

impl<T> ::std::fmt::Debug for BitFlags<T>
    where T: EnumFlagSize,
          T::Size: ::std::fmt::Debug
{
    fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        fmt.write_str(&format!("BitFlags {o} {inner:?} {c} ",
                               o = "{",
                               inner = self.val,
                               c = "}"))
    }
}

impl<T> BitFlags<T>
    where T: EnumFlagSize
{
    /// Create a new BitFlags unsafely. Consider using `from_bits` or `from_bits_truncate`.
    pub unsafe fn new(val: T::Size) -> Self {
        BitFlags { val: val }
    }
}

impl<T> BitFlags<T>
    where T: EnumFlagSize,
          T::Size: InnerBitFlags + Into<BitFlags<T>>
{
    /// Create an empty BitFlags. Empty means `0`.
    pub fn empty() -> Self {
        T::Size::empty().into()
    }

    /// Sets all flags.
    pub fn all() -> Self {
        T::Size::all().into()
    }

    /// Returns true if all flags are set
    pub fn is_all(self) -> bool {
        self.val.is_all()
    }

    /// Returns true if no flag is set
    pub fn is_empty(self) -> bool {
        self.val.is_empty()
    }

    /// Returns the underlying type value
    pub fn bits(self) -> <T::Size as InnerBitFlags>::Type {
        self.val.bits()
    }

    /// Returns true if at least one flag is shared.
    pub fn intersects<B: Into<BitFlags<T>>>(self, other: B) -> bool {
        T::Size::intersects(self.val, other.into().val)
    }

    /// Returns true iff all flags are contained.
    pub fn contains<B: Into<BitFlags<T>>>(self, other: B) -> bool {
        T::Size::contains(self.val, other.into().val)
    }

    /// Flips all flags
    pub fn not(self) -> Self {
        self.val.not().into()
    }

    /// Returns a BitFlags iff the bits value does not contain any illegal flags.
    pub fn from_bits(bits: <T::Size as InnerBitFlags>::Type) -> Option<Self> {
        T::Size::from_bits(bits).map(|v| v.into())
    }

    /// Truncates flags that are illegal
    pub fn from_bits_truncate(bits: <T::Size as InnerBitFlags>::Type) -> Self {
        T::Size::from_bits_truncate(bits).into()
    }

    pub fn toggle(&mut self, other: Self) {
        T::Size::toggle(&mut self.val, other.val);
    }

    pub fn insert(&mut self, other: Self) {
        T::Size::insert(&mut self.val, other.val);
    }

    pub fn remove(&mut self, other: Self) {
        T::Size::remove(&mut self.val, other.val);
    }
}

impl<T> std::cmp::PartialEq for BitFlags<T>
    where T: EnumFlagSize
{
    fn eq(&self, other: &Self) -> bool {
        self.val == other.val
    }
}

// impl<T> std::ops::BitOr for BitFlags<T>
//    where T: EnumFlagSize ,
//          T::Size: BitOr<T::Size, Output = T::Size> + Into<BitFlags<T>>
// {
//    type Output = BitFlags<T>;
//    fn bitor(self, other: Self) -> BitFlags<T> {
//        (self.val | other.val).into()
//    }
// }

impl<T, B> std::ops::BitOr<B> for BitFlags<T>
    where T: EnumFlagSize,
          B: Into<BitFlags<T>>,
          T::Size: BitOr<T::Size, Output = T::Size> + Into<BitFlags<T>>
{
    type Output = BitFlags<T>;
    fn bitor(self, other: B) -> BitFlags<T> {
        (self.val | other.into().val).into()
    }
}

impl<T, B> std::ops::BitAnd<B> for BitFlags<T>
    where T: EnumFlagSize,
          B: Into<BitFlags<T>>,
          T::Size: BitAnd<T::Size, Output = T::Size> + Into<BitFlags<T>>
{
    type Output = BitFlags<T>;
    fn bitand(self, other: B) -> BitFlags<T> {
        (self.val & other.into().val).into()
    }
}

impl<T, B> std::ops::BitXor<B> for BitFlags<T>
    where T: EnumFlagSize,
          B: Into<BitFlags<T>>,
          T::Size: BitXor<T::Size, Output = T::Size> + Into<BitFlags<T>>
{
    type Output = BitFlags<T>;
    fn bitxor(self, other: B) -> BitFlags<T> {
        (self.val ^ other.into().val).into()
    }
}

impl<T> std::ops::Not for BitFlags<T>
    where T: EnumFlagSize,
          T::Size: Not<Output = T::Size> + Into<BitFlags<T>>
{
    type Output = BitFlags<T>;
    fn not(self) -> BitFlags<T> {
        (!self.val).into()
    }
}