about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2014-07-31 09:31:37 +0000
committerbors <bors@rust-lang.org>2014-07-31 09:31:37 +0000
commit6f833ee151bcfcd31c13a15daab78f5fb900bf76 (patch)
tree8b6cd5bba124353216eefac21ffe7ae7fbf879f8 /src/libstd
parent28ae6f53299495019b6bac8c1bba2bcbb19de15c (diff)
parent96d6126f9bae9a9171b150fb9a6e26047229faaa (diff)
downloadrust-6f833ee151bcfcd31c13a15daab78f5fb900bf76.tar.gz
rust-6f833ee151bcfcd31c13a15daab78f5fb900bf76.zip
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.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/bitflags.rs41
-rw-r--r--src/libstd/io/mod.rs7
2 files changed, 46 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..f1b92b973c8 100644
--- a/src/libstd/io/mod.rs
+++ b/src/libstd/io/mod.rs
@@ -223,6 +223,7 @@ responding to errors that may occur while attempting to read the numbers.
 
 use char::Char;
 use collections::Collection;
+use default::Default;
 use fmt;
 use int;
 use iter::Iterator;
@@ -1795,7 +1796,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,
@@ -1830,6 +1830,11 @@ on unix-like systems."]
     }
 )
 
+impl Default for FilePermission {
+    #[inline]
+    fn default() -> FilePermission { FilePermission::empty() }
+}
+
 #[cfg(test)]
 mod tests {
     use super::{IoResult, Reader, MemReader, NoProgress, InvalidInput};