Skip to content

Commit f3e0db1

Browse files
committed
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]
1 parent 72e2c7d commit f3e0db1

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/libstd/bitflags.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ macro_rules! bitflags(
113113
($(#[$attr:meta])* flags $BitFlags:ident: $T:ty {
114114
$($(#[$Flag_attr:meta])* static $Flag:ident = $value:expr),+
115115
}) => (
116-
#[deriving(PartialEq, Eq, Clone)]
116+
#[deriving(PartialEq, Eq, Clone, PartialOrd, Ord, Hash)]
117117
$(#[$attr])*
118118
pub struct $BitFlags {
119119
bits: $T,
@@ -220,6 +220,7 @@ macro_rules! bitflags(
220220

221221
#[cfg(test)]
222222
mod tests {
223+
use hash;
223224
use option::{Some, None};
224225
use ops::{BitOr, BitAnd, Sub, Not};
225226

@@ -336,4 +337,42 @@ mod tests {
336337
assert!((e1 - e2) == FlagA); // set difference
337338
assert!(!e2 == FlagA); // set complement
338339
}
340+
341+
#[test]
342+
fn test_lt() {
343+
let mut a = Flags::empty();
344+
let mut b = Flags::empty();
345+
346+
assert!(!(a < b) && !(b < a));
347+
b = FlagB;
348+
assert!(a < b);
349+
a = FlagC;
350+
assert!(!(a < b) && b < a);
351+
b = FlagC | FlagB;
352+
assert!(a < b);
353+
}
354+
355+
#[test]
356+
fn test_ord() {
357+
let mut a = Flags::empty();
358+
let mut b = Flags::empty();
359+
360+
assert!(a <= b && a >= b);
361+
a = FlagA;
362+
assert!(a > b && a >= b);
363+
assert!(b < a && b <= a);
364+
b = FlagB;
365+
assert!(b > a && b >= a);
366+
assert!(a < b && a <= b);
367+
}
368+
369+
#[test]
370+
fn test_hash() {
371+
let mut x = Flags::empty();
372+
let mut y = Flags::empty();
373+
assert!(hash::hash(&x) == hash::hash(&y));
374+
x = Flags::all();
375+
y = FlagABC;
376+
assert!(hash::hash(&x) == hash::hash(&y));
377+
}
339378
}

src/libstd/io/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,6 @@ pub struct UnstableFileStat {
17951795
bitflags!(
17961796
#[doc="A set of permissions for a file or directory is represented
17971797
by a set of flags which are or'd together."]
1798-
#[deriving(Hash)]
17991798
#[deriving(Show)]
18001799
flags FilePermission: u32 {
18011800
static UserRead = 0o400,

0 commit comments

Comments
 (0)