Merge pull request #13 from chris-morgan/master

Implement FromIterator on the flags struct.
This commit is contained in:
Alex Crichton
2015-06-24 23:00:35 -07:00
+29 -6
View File
@@ -87,14 +87,20 @@
/// Attributes can be attached to the generated `struct` by placing them
/// before the `flags` keyword.
///
/// # Derived traits
/// # Trait implementations
///
/// The `PartialEq` and `Clone` traits are automatically derived for the
/// `struct` using the `deriving` attribute. Additional traits can be derived by
/// providing an explicit `deriving` attribute on `flags`. The `Debug` trait is
/// also implemented by displaying the bits value of the internal struct.
/// The `Copy`, `Clone`, `PartialEq`, `Eq`, `PartialOrd`, `Ord` and `Hash`
/// traits automatically derived for the `struct` using the `derive` attribute.
/// Additional traits can be derived by providing an explicit `derive`
/// attribute on `flags`.
///
/// # Operators
/// The `FromIterator` trait is implemented for the `struct`, too, calculating
/// the union of the instances of the `struct` iterated over.
///
/// The `Debug` trait is also implemented by displaying the bits value of the
/// internal struct.
///
/// ## Operators
///
/// The following operator traits are implemented for the generated `struct`:
///
@@ -274,6 +280,16 @@ macro_rules! bitflags {
$BitFlags { bits: !self.bits } & $BitFlags::all()
}
}
impl ::std::iter::FromIterator<$BitFlags> for $BitFlags {
fn from_iter<T: ::std::iter::IntoIterator<Item=$BitFlags>>(iterator: T) -> $BitFlags {
let mut result = Self::empty();
for item in iterator {
result.insert(item)
}
result
}
}
};
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
$($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+,
@@ -460,6 +476,13 @@ mod tests {
assert!(m4 == AnotherSetOfFlags::empty());
}
#[test]
fn test_from_iterator() {
assert_eq!([].iter().cloned().collect::<Flags>(), Flags::empty());
assert_eq!([FlagA, FlagB].iter().cloned().collect::<Flags>(), FlagA | FlagB);
assert_eq!([FlagA, FlagABC].iter().cloned().collect::<Flags>(), FlagABC);
}
#[test]
fn test_lt() {
let mut a = Flags::empty();