Skip to content

Commit 6f833ee

Browse files
committed
auto merge of #16074 : nham/rust/bitflags_traits, r=alexcrichton
I wanted to add an implementation of `Default` inside the bitflags macro, but `Default` isn't in the prelude, which means anyone who wants to use `bitflags!` needs to import it. This seems not nice, so I've just implemented for `FilePermission` instead.
2 parents 28ae6f5 + 96d6126 commit 6f833ee

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-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

+6-1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ responding to errors that may occur while attempting to read the numbers.
223223

224224
use char::Char;
225225
use collections::Collection;
226+
use default::Default;
226227
use fmt;
227228
use int;
228229
use iter::Iterator;
@@ -1795,7 +1796,6 @@ pub struct UnstableFileStat {
17951796
bitflags!(
17961797
#[doc="A set of permissions for a file or directory is represented
17971798
by a set of flags which are or'd together."]
1798-
#[deriving(Hash)]
17991799
#[deriving(Show)]
18001800
flags FilePermission: u32 {
18011801
static UserRead = 0o400,
@@ -1830,6 +1830,11 @@ on unix-like systems."]
18301830
}
18311831
)
18321832

1833+
impl Default for FilePermission {
1834+
#[inline]
1835+
fn default() -> FilePermission { FilePermission::empty() }
1836+
}
1837+
18331838
#[cfg(test)]
18341839
mod tests {
18351840
use super::{IoResult, Reader, MemReader, NoProgress, InvalidInput};

0 commit comments

Comments
 (0)