diff options
| author | nham <hamann.nick@gmail.com> | 2014-07-30 15:53:40 -0400 |
|---|---|---|
| committer | nham <hamann.nick@gmail.com> | 2014-07-30 16:04:33 -0400 |
| commit | f3e0db15592d71b8b7125bffbf73df231be8da9d (patch) | |
| tree | 2633af8d93efcc10e15dbe1e3fb30c39b99fda3e /src/libstd | |
| parent | 72e2c7dec34bd87fab6bb15bb7d3d97269d4afd3 (diff) | |
| download | rust-f3e0db15592d71b8b7125bffbf73df231be8da9d.tar.gz rust-f3e0db15592d71b8b7125bffbf73df231be8da9d.zip | |
Derive PartialOrd, Ord and Hash for bitflags types.
In order to prevent users from having to manually implement Hash and Ord for bitflags types, this commit derives these traits automatically. This breaks code that has manually implemented any of these traits for types created by the bitflags! macro. Change this code by removing implementations of these traits. [breaking-change]
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/bitflags.rs | 41 | ||||
| -rw-r--r-- | src/libstd/io/mod.rs | 1 |
2 files changed, 40 insertions, 2 deletions
diff --git a/src/libstd/bitflags.rs b/src/libstd/bitflags.rs index 834d461f20b..d231189aede 100644 --- a/src/libstd/bitflags.rs +++ b/src/libstd/bitflags.rs @@ -113,7 +113,7 @@ macro_rules! bitflags( ($(#[$attr:meta])* flags $BitFlags:ident: $T:ty { $($(#[$Flag_attr:meta])* static $Flag:ident = $value:expr),+ }) => ( - #[deriving(PartialEq, Eq, Clone)] + #[deriving(PartialEq, Eq, Clone, PartialOrd, Ord, Hash)] $(#[$attr])* pub struct $BitFlags { bits: $T, @@ -220,6 +220,7 @@ macro_rules! bitflags( #[cfg(test)] mod tests { + use hash; use option::{Some, None}; use ops::{BitOr, BitAnd, Sub, Not}; @@ -336,4 +337,42 @@ mod tests { assert!((e1 - e2) == FlagA); // set difference assert!(!e2 == FlagA); // set complement } + + #[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(); + assert!(hash::hash(&x) == hash::hash(&y)); + x = Flags::all(); + y = FlagABC; + assert!(hash::hash(&x) == hash::hash(&y)); + } } diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index 4277b509962..6f57048c613 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -1795,7 +1795,6 @@ pub struct UnstableFileStat { bitflags!( #[doc="A set of permissions for a file or directory is represented by a set of flags which are or'd together."] - #[deriving(Hash)] #[deriving(Show)] flags FilePermission: u32 { static UserRead = 0o400, |
