2014-03-22 19:38:15 +11:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-01-16 12:20:03 -08:00
|
|
|
#![crate_name = "rustc_bitflags"]
|
2015-01-21 18:21:14 -08:00
|
|
|
#![allow(unknown_features)]
|
|
|
|
#![feature(staged_api)]
|
2015-01-16 12:20:03 -08:00
|
|
|
#![staged_api]
|
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![no_std]
|
2015-01-22 18:22:03 -08:00
|
|
|
#![unstable(feature = "rustc_private")]
|
2014-05-02 09:41:34 -07:00
|
|
|
|
2014-09-04 15:25:22 +10:00
|
|
|
//! A typesafe bitmask flag generator.
|
|
|
|
|
2015-01-16 12:20:03 -08:00
|
|
|
#[cfg(test)] #[macro_use] extern crate std;
|
|
|
|
|
2014-09-04 15:25:22 +10:00
|
|
|
/// The `bitflags!` macro generates a `struct` that holds a set of C-style
|
|
|
|
/// bitmask flags. It is useful for creating typesafe wrappers for C APIs.
|
|
|
|
///
|
|
|
|
/// The flags should only be defined for integer types, otherwise unexpected
|
|
|
|
/// type errors may occur at compile time.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2014-09-16 13:27:34 +02:00
|
|
|
/// ```{.rust}
|
2015-01-16 12:20:03 -08:00
|
|
|
/// #[macro_use] extern crate rustc_bitflags;
|
|
|
|
///
|
2014-09-04 16:35:06 +10:00
|
|
|
/// bitflags! {
|
2014-09-04 15:25:22 +10:00
|
|
|
/// flags Flags: u32 {
|
2014-12-20 16:07:03 +08:00
|
|
|
/// const FLAG_A = 0b00000001,
|
|
|
|
/// const FLAG_B = 0b00000010,
|
|
|
|
/// const FLAG_C = 0b00000100,
|
2014-10-06 16:29:47 -07:00
|
|
|
/// const FLAG_ABC = FLAG_A.bits
|
|
|
|
/// | FLAG_B.bits
|
|
|
|
/// | FLAG_C.bits,
|
2014-09-04 15:25:22 +10:00
|
|
|
/// }
|
2014-09-04 16:35:06 +10:00
|
|
|
/// }
|
2014-09-04 15:25:22 +10:00
|
|
|
///
|
|
|
|
/// fn main() {
|
2014-10-06 16:08:35 +13:00
|
|
|
/// let e1 = FLAG_A | FLAG_C;
|
|
|
|
/// let e2 = FLAG_B | FLAG_C;
|
|
|
|
/// assert!((e1 | e2) == FLAG_ABC); // union
|
|
|
|
/// assert!((e1 & e2) == FLAG_C); // intersection
|
|
|
|
/// assert!((e1 - e2) == FLAG_A); // set difference
|
|
|
|
/// assert!(!e2 == FLAG_A); // set complement
|
2014-09-04 15:25:22 +10:00
|
|
|
/// }
|
2014-09-16 13:27:34 +02:00
|
|
|
/// ```
|
2014-09-04 15:25:22 +10:00
|
|
|
///
|
|
|
|
/// The generated `struct`s can also be extended with type and trait implementations:
|
|
|
|
///
|
2014-09-16 13:27:34 +02:00
|
|
|
/// ```{.rust}
|
2015-01-16 12:20:03 -08:00
|
|
|
/// #[macro_use] extern crate rustc_bitflags;
|
|
|
|
///
|
2014-09-04 15:25:22 +10:00
|
|
|
/// use std::fmt;
|
|
|
|
///
|
2014-09-04 16:35:06 +10:00
|
|
|
/// bitflags! {
|
2014-09-04 15:25:22 +10:00
|
|
|
/// flags Flags: u32 {
|
2014-12-20 16:07:03 +08:00
|
|
|
/// const FLAG_A = 0b00000001,
|
|
|
|
/// const FLAG_B = 0b00000010,
|
2014-09-04 15:25:22 +10:00
|
|
|
/// }
|
2014-09-04 16:35:06 +10:00
|
|
|
/// }
|
2014-09-04 15:25:22 +10:00
|
|
|
///
|
|
|
|
/// impl Flags {
|
|
|
|
/// pub fn clear(&mut self) {
|
|
|
|
/// self.bits = 0; // The `bits` field can be accessed from within the
|
|
|
|
/// // same module where the `bitflags!` macro was invoked.
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2015-01-28 08:34:18 -05:00
|
|
|
/// impl fmt::Debug for Flags {
|
2014-09-04 15:25:22 +10:00
|
|
|
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
/// write!(f, "hi!")
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2014-10-06 16:08:35 +13:00
|
|
|
/// let mut flags = FLAG_A | FLAG_B;
|
2014-09-04 15:25:22 +10:00
|
|
|
/// flags.clear();
|
|
|
|
/// assert!(flags.is_empty());
|
2015-01-06 16:16:35 -08:00
|
|
|
/// assert_eq!(format!("{:?}", flags).as_slice(), "hi!");
|
2014-09-04 15:25:22 +10:00
|
|
|
/// }
|
2014-09-16 13:27:34 +02:00
|
|
|
/// ```
|
2014-09-04 15:25:22 +10:00
|
|
|
///
|
|
|
|
/// # Attributes
|
|
|
|
///
|
|
|
|
/// Attributes can be attached to the generated `struct` by placing them
|
|
|
|
/// before the `flags` keyword.
|
|
|
|
///
|
|
|
|
/// # Derived traits
|
|
|
|
///
|
|
|
|
/// 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`.
|
|
|
|
///
|
|
|
|
/// # Operators
|
|
|
|
///
|
|
|
|
/// The following operator traits are implemented for the generated `struct`:
|
|
|
|
///
|
|
|
|
/// - `BitOr`: union
|
|
|
|
/// - `BitAnd`: intersection
|
2014-09-25 18:08:49 -07:00
|
|
|
/// - `BitXor`: toggle
|
2014-09-04 15:25:22 +10:00
|
|
|
/// - `Sub`: set difference
|
|
|
|
/// - `Not`: set complement
|
|
|
|
///
|
|
|
|
/// # Methods
|
|
|
|
///
|
|
|
|
/// The following methods are defined for the generated `struct`:
|
|
|
|
///
|
|
|
|
/// - `empty`: an empty set of flags
|
|
|
|
/// - `all`: the set of all flags
|
|
|
|
/// - `bits`: the raw value of the flags currently stored
|
2014-12-27 20:13:32 -05:00
|
|
|
/// - `from_bits`: convert from underlying bit representation, unless that
|
|
|
|
/// representation contains bits that do not correspond to a flag
|
|
|
|
/// - `from_bits_truncate`: convert from underlying bit representation, dropping
|
|
|
|
/// any bits that do not correspond to flags
|
2014-09-04 15:25:22 +10:00
|
|
|
/// - `is_empty`: `true` if no flags are currently stored
|
|
|
|
/// - `is_all`: `true` if all flags are currently set
|
|
|
|
/// - `intersects`: `true` if there are flags common to both `self` and `other`
|
|
|
|
/// - `contains`: `true` all of the flags in `other` are contained within `self`
|
|
|
|
/// - `insert`: inserts the specified flags in-place
|
|
|
|
/// - `remove`: removes the specified flags in-place
|
2014-09-25 18:08:49 -07:00
|
|
|
/// - `toggle`: the specified flags will be inserted if not present, and removed
|
|
|
|
/// if they are.
|
2014-03-22 19:38:15 +11:00
|
|
|
#[macro_export]
|
2014-09-04 16:35:06 +10:00
|
|
|
macro_rules! bitflags {
|
2014-05-02 09:41:34 -07:00
|
|
|
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
|
2014-10-06 16:29:47 -07:00
|
|
|
$($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+
|
2014-09-04 16:35:06 +10:00
|
|
|
}) => {
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Copy, PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
|
2014-05-02 09:41:34 -07:00
|
|
|
$(#[$attr])*
|
2014-03-22 19:38:15 +11:00
|
|
|
pub struct $BitFlags {
|
2014-04-30 10:02:11 -07:00
|
|
|
bits: $T,
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
|
|
|
|
2014-10-06 16:29:47 -07:00
|
|
|
$($(#[$Flag_attr])* pub const $Flag: $BitFlags = $BitFlags { bits: $value };)+
|
2014-03-22 19:38:15 +11:00
|
|
|
|
|
|
|
impl $BitFlags {
|
|
|
|
/// Returns an empty set of flags.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-03-22 19:38:15 +11:00
|
|
|
pub fn empty() -> $BitFlags {
|
|
|
|
$BitFlags { bits: 0 }
|
|
|
|
}
|
|
|
|
|
2014-05-07 01:54:44 -04:00
|
|
|
/// Returns the set containing all flags.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-05-07 01:54:44 -04:00
|
|
|
pub fn all() -> $BitFlags {
|
|
|
|
$BitFlags { bits: $($value)|+ }
|
|
|
|
}
|
|
|
|
|
2014-03-22 19:38:15 +11:00
|
|
|
/// Returns the raw value of the flags currently stored.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-03-22 19:38:15 +11:00
|
|
|
pub fn bits(&self) -> $T {
|
|
|
|
self.bits
|
|
|
|
}
|
|
|
|
|
2014-05-14 14:39:16 -07:00
|
|
|
/// Convert from underlying bit representation, unless that
|
|
|
|
/// representation contains bits that do not correspond to a flag.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-05-14 14:39:16 -07:00
|
|
|
pub fn from_bits(bits: $T) -> ::std::option::Option<$BitFlags> {
|
|
|
|
if (bits & !$BitFlags::all().bits()) != 0 {
|
2014-11-28 11:57:41 -05:00
|
|
|
::std::option::Option::None
|
2014-05-14 14:39:16 -07:00
|
|
|
} else {
|
2014-11-28 11:57:41 -05:00
|
|
|
::std::option::Option::Some($BitFlags { bits: bits })
|
2014-05-14 14:39:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Convert from underlying bit representation, dropping any bits
|
|
|
|
/// that do not correspond to flags.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-05-14 14:39:16 -07:00
|
|
|
pub fn from_bits_truncate(bits: $T) -> $BitFlags {
|
|
|
|
$BitFlags { bits: bits } & $BitFlags::all()
|
2014-05-02 10:41:07 -07:00
|
|
|
}
|
|
|
|
|
2014-03-22 19:38:15 +11:00
|
|
|
/// Returns `true` if no flags are currently stored.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-03-22 19:38:15 +11:00
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
*self == $BitFlags::empty()
|
|
|
|
}
|
|
|
|
|
2014-05-07 01:54:44 -04:00
|
|
|
/// Returns `true` if all flags are currently set.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-05-07 01:54:44 -04:00
|
|
|
pub fn is_all(&self) -> bool {
|
|
|
|
*self == $BitFlags::all()
|
|
|
|
}
|
|
|
|
|
2014-03-22 19:38:15 +11:00
|
|
|
/// Returns `true` if there are flags common to both `self` and `other`.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-03-22 19:38:15 +11:00
|
|
|
pub fn intersects(&self, other: $BitFlags) -> bool {
|
2014-10-31 05:40:15 -04:00
|
|
|
!(*self & other).is_empty()
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` all of the flags in `other` are contained within `self`.
|
2014-09-14 20:07:45 +02:00
|
|
|
#[inline]
|
2014-03-22 19:38:15 +11:00
|
|
|
pub fn contains(&self, other: $BitFlags) -> bool {
|
2014-10-31 05:40:15 -04:00
|
|
|
(*self & other) == other
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Inserts the specified flags in-place.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-03-22 19:38:15 +11:00
|
|
|
pub fn insert(&mut self, other: $BitFlags) {
|
|
|
|
self.bits |= other.bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes the specified flags in-place.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-03-22 19:38:15 +11:00
|
|
|
pub fn remove(&mut self, other: $BitFlags) {
|
|
|
|
self.bits &= !other.bits;
|
|
|
|
}
|
2014-09-25 18:08:49 -07:00
|
|
|
|
|
|
|
/// Toggles the specified flags in-place.
|
2014-10-16 15:25:46 -07:00
|
|
|
#[inline]
|
2014-09-25 18:08:49 -07:00
|
|
|
pub fn toggle(&mut self, other: $BitFlags) {
|
|
|
|
self.bits ^= other.bits;
|
|
|
|
}
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
|
|
|
|
2014-12-31 15:45:13 -05:00
|
|
|
impl ::std::ops::BitOr for $BitFlags {
|
|
|
|
type Output = $BitFlags;
|
|
|
|
|
2014-12-01 14:29:47 -05:00
|
|
|
/// Returns the union of the two sets of flags.
|
|
|
|
#[inline]
|
|
|
|
fn bitor(self, other: $BitFlags) -> $BitFlags {
|
|
|
|
$BitFlags { bits: self.bits | other.bits }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 15:45:13 -05:00
|
|
|
impl ::std::ops::BitXor for $BitFlags {
|
|
|
|
type Output = $BitFlags;
|
|
|
|
|
2014-12-01 14:29:47 -05:00
|
|
|
/// Returns the left flags, but with all the right flags toggled.
|
|
|
|
#[inline]
|
|
|
|
fn bitxor(self, other: $BitFlags) -> $BitFlags {
|
|
|
|
$BitFlags { bits: self.bits ^ other.bits }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 15:45:13 -05:00
|
|
|
impl ::std::ops::BitAnd for $BitFlags {
|
|
|
|
type Output = $BitFlags;
|
|
|
|
|
2014-12-01 14:29:47 -05:00
|
|
|
/// Returns the intersection between the two sets of flags.
|
|
|
|
#[inline]
|
|
|
|
fn bitand(self, other: $BitFlags) -> $BitFlags {
|
|
|
|
$BitFlags { bits: self.bits & other.bits }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 15:45:13 -05:00
|
|
|
impl ::std::ops::Sub for $BitFlags {
|
|
|
|
type Output = $BitFlags;
|
|
|
|
|
2014-12-01 14:29:47 -05:00
|
|
|
/// Returns the set difference of the two sets of flags.
|
|
|
|
#[inline]
|
|
|
|
fn sub(self, other: $BitFlags) -> $BitFlags {
|
|
|
|
$BitFlags { bits: self.bits & !other.bits }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-02 22:56:24 -05:00
|
|
|
impl ::std::ops::Not for $BitFlags {
|
|
|
|
type Output = $BitFlags;
|
|
|
|
|
2014-12-15 17:06:10 -05:00
|
|
|
/// Returns the complement of this set of flags.
|
|
|
|
#[inline]
|
|
|
|
fn not(self) -> $BitFlags {
|
|
|
|
$BitFlags { bits: !self.bits } & $BitFlags::all()
|
|
|
|
}
|
|
|
|
}
|
2014-09-04 16:35:06 +10:00
|
|
|
};
|
2014-09-04 15:16:04 +10:00
|
|
|
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
|
2014-10-06 16:29:47 -07:00
|
|
|
$($(#[$Flag_attr:meta])* const $Flag:ident = $value:expr),+,
|
2014-09-04 16:35:06 +10:00
|
|
|
}) => {
|
|
|
|
bitflags! {
|
2014-09-04 15:16:04 +10:00
|
|
|
$(#[$attr])*
|
2014-09-19 21:08:08 -07:00
|
|
|
flags $BitFlags: $T {
|
2014-10-06 16:29:47 -07:00
|
|
|
$($(#[$Flag_attr])* const $Flag = $value),+
|
2014-09-04 15:16:04 +10:00
|
|
|
}
|
2014-09-04 16:35:06 +10:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2014-03-22 19:38:15 +11:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2014-10-27 15:37:07 -07:00
|
|
|
#[allow(non_upper_case_globals)]
|
2014-03-22 19:38:15 +11:00
|
|
|
mod tests {
|
2015-01-16 12:20:03 -08:00
|
|
|
use std::hash::{self, SipHasher};
|
|
|
|
use std::option::Option::{Some, None};
|
2014-04-29 20:05:51 -07:00
|
|
|
|
2014-09-04 16:35:06 +10:00
|
|
|
bitflags! {
|
2014-09-04 15:22:16 +10:00
|
|
|
#[doc = "> The first principle is that you must not fool yourself — and"]
|
|
|
|
#[doc = "> you are the easiest person to fool."]
|
|
|
|
#[doc = "> "]
|
|
|
|
#[doc = "> - Richard Feynman"]
|
2014-05-02 09:41:34 -07:00
|
|
|
flags Flags: u32 {
|
2014-12-20 16:07:03 +08:00
|
|
|
const FlagA = 0b00000001,
|
2014-09-04 15:22:16 +10:00
|
|
|
#[doc = "<pcwalton> macros are way better at generating code than trans is"]
|
2014-12-20 16:07:03 +08:00
|
|
|
const FlagB = 0b00000010,
|
|
|
|
const FlagC = 0b00000100,
|
2014-09-04 15:22:16 +10:00
|
|
|
#[doc = "* cmr bed"]
|
|
|
|
#[doc = "* strcat table"]
|
|
|
|
#[doc = "<strcat> wait what?"]
|
2014-10-06 16:29:47 -07:00
|
|
|
const FlagABC = FlagA.bits
|
2014-05-02 09:41:34 -07:00
|
|
|
| FlagB.bits
|
2014-09-04 15:16:04 +10:00
|
|
|
| FlagC.bits,
|
2014-05-02 09:41:34 -07:00
|
|
|
}
|
2014-09-04 16:35:06 +10:00
|
|
|
}
|
2014-03-22 19:38:15 +11:00
|
|
|
|
2014-09-19 21:08:08 -07:00
|
|
|
bitflags! {
|
2014-10-05 18:11:17 +08:00
|
|
|
flags AnotherSetOfFlags: i8 {
|
|
|
|
const AnotherFlag = -1_i8,
|
2014-09-19 21:08:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 19:38:15 +11:00
|
|
|
#[test]
|
|
|
|
fn test_bits(){
|
2014-12-20 16:07:03 +08:00
|
|
|
assert_eq!(Flags::empty().bits(), 0b00000000);
|
|
|
|
assert_eq!(FlagA.bits(), 0b00000001);
|
|
|
|
assert_eq!(FlagABC.bits(), 0b00000111);
|
2014-10-05 18:11:17 +08:00
|
|
|
|
2014-12-20 16:07:03 +08:00
|
|
|
assert_eq!(AnotherSetOfFlags::empty().bits(), 0b00);
|
2014-10-05 18:11:17 +08:00
|
|
|
assert_eq!(AnotherFlag.bits(), !0_i8);
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
|
|
|
|
2014-05-13 23:12:55 +09:00
|
|
|
#[test]
|
|
|
|
fn test_from_bits() {
|
2014-05-14 14:39:16 -07:00
|
|
|
assert!(Flags::from_bits(0) == Some(Flags::empty()));
|
2014-12-20 16:07:03 +08:00
|
|
|
assert!(Flags::from_bits(0b1) == Some(FlagA));
|
|
|
|
assert!(Flags::from_bits(0b10) == Some(FlagB));
|
|
|
|
assert!(Flags::from_bits(0b11) == Some(FlagA | FlagB));
|
|
|
|
assert!(Flags::from_bits(0b1000) == None);
|
2014-10-05 18:11:17 +08:00
|
|
|
|
|
|
|
assert!(AnotherSetOfFlags::from_bits(!0_i8) == Some(AnotherFlag));
|
2014-05-14 14:39:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_bits_truncate() {
|
|
|
|
assert!(Flags::from_bits_truncate(0) == Flags::empty());
|
2014-12-20 16:07:03 +08:00
|
|
|
assert!(Flags::from_bits_truncate(0b1) == FlagA);
|
|
|
|
assert!(Flags::from_bits_truncate(0b10) == FlagB);
|
|
|
|
assert!(Flags::from_bits_truncate(0b11) == (FlagA | FlagB));
|
|
|
|
assert!(Flags::from_bits_truncate(0b1000) == Flags::empty());
|
|
|
|
assert!(Flags::from_bits_truncate(0b1001) == FlagA);
|
2014-10-05 18:11:17 +08:00
|
|
|
|
|
|
|
assert!(AnotherSetOfFlags::from_bits_truncate(0_i8) == AnotherSetOfFlags::empty());
|
2014-05-13 23:12:55 +09:00
|
|
|
}
|
|
|
|
|
2014-03-22 19:38:15 +11:00
|
|
|
#[test]
|
|
|
|
fn test_is_empty(){
|
|
|
|
assert!(Flags::empty().is_empty());
|
|
|
|
assert!(!FlagA.is_empty());
|
|
|
|
assert!(!FlagABC.is_empty());
|
2014-10-05 18:11:17 +08:00
|
|
|
|
|
|
|
assert!(!AnotherFlag.is_empty());
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
|
|
|
|
2014-05-07 01:54:44 -04:00
|
|
|
#[test]
|
|
|
|
fn test_is_all() {
|
|
|
|
assert!(Flags::all().is_all());
|
|
|
|
assert!(!FlagA.is_all());
|
|
|
|
assert!(FlagABC.is_all());
|
2014-10-05 18:11:17 +08:00
|
|
|
|
|
|
|
assert!(AnotherFlag.is_all());
|
2014-05-07 01:54:44 -04:00
|
|
|
}
|
|
|
|
|
2014-03-22 19:38:15 +11:00
|
|
|
#[test]
|
|
|
|
fn test_two_empties_do_not_intersect() {
|
|
|
|
let e1 = Flags::empty();
|
|
|
|
let e2 = Flags::empty();
|
|
|
|
assert!(!e1.intersects(e2));
|
2014-10-05 18:11:17 +08:00
|
|
|
|
|
|
|
assert!(AnotherFlag.intersects(AnotherFlag));
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_does_not_intersect_with_full() {
|
|
|
|
let e1 = Flags::empty();
|
|
|
|
let e2 = FlagABC;
|
|
|
|
assert!(!e1.intersects(e2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_disjoint_intersects() {
|
|
|
|
let e1 = FlagA;
|
|
|
|
let e2 = FlagB;
|
|
|
|
assert!(!e1.intersects(e2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_overlapping_intersects() {
|
|
|
|
let e1 = FlagA;
|
|
|
|
let e2 = FlagA | FlagB;
|
|
|
|
assert!(e1.intersects(e2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_contains() {
|
|
|
|
let e1 = FlagA;
|
|
|
|
let e2 = FlagA | FlagB;
|
|
|
|
assert!(!e1.contains(e2));
|
|
|
|
assert!(e2.contains(e1));
|
|
|
|
assert!(FlagABC.contains(e2));
|
2014-10-05 18:11:17 +08:00
|
|
|
|
|
|
|
assert!(AnotherFlag.contains(AnotherFlag));
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_insert(){
|
|
|
|
let mut e1 = FlagA;
|
|
|
|
let e2 = FlagA | FlagB;
|
|
|
|
e1.insert(e2);
|
|
|
|
assert!(e1 == e2);
|
2014-10-05 18:11:17 +08:00
|
|
|
|
|
|
|
let mut e3 = AnotherSetOfFlags::empty();
|
|
|
|
e3.insert(AnotherFlag);
|
|
|
|
assert!(e3 == AnotherFlag);
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_remove(){
|
|
|
|
let mut e1 = FlagA | FlagB;
|
|
|
|
let e2 = FlagA | FlagC;
|
|
|
|
e1.remove(e2);
|
|
|
|
assert!(e1 == FlagB);
|
2014-10-05 18:11:17 +08:00
|
|
|
|
|
|
|
let mut e3 = AnotherFlag;
|
|
|
|
e3.remove(AnotherFlag);
|
|
|
|
assert!(e3 == AnotherSetOfFlags::empty());
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_operators() {
|
|
|
|
let e1 = FlagA | FlagC;
|
|
|
|
let e2 = FlagB | FlagC;
|
2014-09-25 18:08:49 -07:00
|
|
|
assert!((e1 | e2) == FlagABC); // union
|
|
|
|
assert!((e1 & e2) == FlagC); // intersection
|
|
|
|
assert!((e1 - e2) == FlagA); // set difference
|
|
|
|
assert!(!e2 == FlagA); // set complement
|
|
|
|
assert!(e1 ^ e2 == FlagA | FlagB); // toggle
|
|
|
|
let mut e3 = e1;
|
|
|
|
e3.toggle(e2);
|
|
|
|
assert!(e3 == FlagA | FlagB);
|
2014-10-05 18:11:17 +08:00
|
|
|
|
|
|
|
let mut m4 = AnotherSetOfFlags::empty();
|
|
|
|
m4.toggle(AnotherSetOfFlags::empty());
|
|
|
|
assert!(m4 == AnotherSetOfFlags::empty());
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|
2014-07-30 15:53:40 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lt() {
|
|
|
|
let mut a = Flags::empty();
|
|
|
|
let mut b = Flags::empty();
|
|
|
|
|
|
|
|
assert!(!(a < b) && !(b < a));
|
|
|
|
b = FlagB;
|
|
|
|
assert!(a < b);
|
|
|
|
a = FlagC;
|
|
|
|
assert!(!(a < b) && b < a);
|
|
|
|
b = FlagC | FlagB;
|
|
|
|
assert!(a < b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ord() {
|
|
|
|
let mut a = Flags::empty();
|
|
|
|
let mut b = Flags::empty();
|
|
|
|
|
|
|
|
assert!(a <= b && a >= b);
|
|
|
|
a = FlagA;
|
|
|
|
assert!(a > b && a >= b);
|
|
|
|
assert!(b < a && b <= a);
|
|
|
|
b = FlagB;
|
|
|
|
assert!(b > a && b >= a);
|
|
|
|
assert!(a < b && a <= b);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hash() {
|
|
|
|
let mut x = Flags::empty();
|
|
|
|
let mut y = Flags::empty();
|
std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
2014-12-09 12:37:23 -08:00
|
|
|
assert!(hash::hash::<Flags, SipHasher>(&x) == hash::hash::<Flags, SipHasher>(&y));
|
2014-07-30 15:53:40 -04:00
|
|
|
x = Flags::all();
|
|
|
|
y = FlagABC;
|
std: Stabilize the std::hash module
This commit aims to prepare the `std::hash` module for alpha by formalizing its
current interface whileholding off on adding `#[stable]` to the new APIs. The
current usage with the `HashMap` and `HashSet` types is also reconciled by
separating out composable parts of the design. The primary goal of this slight
redesign is to separate the concepts of a hasher's state from a hashing
algorithm itself.
The primary change of this commit is to separate the `Hasher` trait into a
`Hasher` and a `HashState` trait. Conceptually the old `Hasher` trait was
actually just a factory for various states, but hashing had very little control
over how these states were used. Additionally the old `Hasher` trait was
actually fairly unrelated to hashing.
This commit redesigns the existing `Hasher` trait to match what the notion of a
`Hasher` normally implies with the following definition:
trait Hasher {
type Output;
fn reset(&mut self);
fn finish(&self) -> Output;
}
This `Hasher` trait emphasizes that hashing algorithms may produce outputs other
than a `u64`, so the output type is made generic. Other than that, however, very
little is assumed about a particular hasher. It is left up to implementors to
provide specific methods or trait implementations to feed data into a hasher.
The corresponding `Hash` trait becomes:
trait Hash<H: Hasher> {
fn hash(&self, &mut H);
}
The old default of `SipState` was removed from this trait as it's not something
that we're willing to stabilize until the end of time, but the type parameter is
always required to implement `Hasher`. Note that the type parameter `H` remains
on the trait to enable multidispatch for specialization of hashing for
particular hashers.
Note that `Writer` is not mentioned in either of `Hash` or `Hasher`, it is
simply used as part `derive` and the implementations for all primitive types.
With these definitions, the old `Hasher` trait is realized as a new `HashState`
trait in the `collections::hash_state` module as an unstable addition for
now. The current definition looks like:
trait HashState {
type Hasher: Hasher;
fn hasher(&self) -> Hasher;
}
The purpose of this trait is to emphasize that the one piece of functionality
for implementors is that new instances of `Hasher` can be created. This
conceptually represents the two keys from which more instances of a
`SipHasher` can be created, and a `HashState` is what's stored in a
`HashMap`, not a `Hasher`.
Implementors of custom hash algorithms should implement the `Hasher` trait, and
only hash algorithms intended for use in hash maps need to implement or worry
about the `HashState` trait.
The entire module and `HashState` infrastructure remains `#[unstable]` due to it
being recently redesigned, but some other stability decision made for the
`std::hash` module are:
* The `Writer` trait remains `#[experimental]` as it's intended to be replaced
with an `io::Writer` (more details soon).
* The top-level `hash` function is `#[unstable]` as it is intended to be generic
over the hashing algorithm instead of hardwired to `SipHasher`
* The inner `sip` module is now private as its one export, `SipHasher` is
reexported in the `hash` module.
And finally, a few changes were made to the default parameters on `HashMap`.
* The `RandomSipHasher` default type parameter was renamed to `RandomState`.
This renaming emphasizes that it is not a hasher, but rather just state to
generate hashers. It also moves away from the name "sip" as it may not always
be implemented as `SipHasher`. This type lives in the
`std::collections::hash_map` module as `#[unstable]`
* The associated `Hasher` type of `RandomState` is creatively called...
`Hasher`! This concrete structure lives next to `RandomState` as an
implemenation of the "default hashing algorithm" used for a `HashMap`. Under
the hood this is currently implemented as `SipHasher`, but it draws an
explicit interface for now and allows us to modify the implementation over
time if necessary.
There are many breaking changes outlined above, and as a result this commit is
a:
[breaking-change]
2014-12-09 12:37:23 -08:00
|
|
|
assert!(hash::hash::<Flags, SipHasher>(&x) == hash::hash::<Flags, SipHasher>(&y));
|
2014-07-30 15:53:40 -04:00
|
|
|
}
|
2014-03-22 19:38:15 +11:00
|
|
|
}
|