about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-06-17 17:05:51 +1000
committerHuon Wilson <dbau.pp+github@gmail.com>2013-06-17 17:05:51 +1000
commitf1d971ae189180cbcf3fd37a35ae3fb183833168 (patch)
tree74274e80f0cf17ed86b195351b77cdebd53569a7 /src/libstd
parentd1a2360b36fc43b818877d80a1bc8fd907032d00 (diff)
downloadrust-f1d971ae189180cbcf3fd37a35ae3fb183833168.tar.gz
rust-f1d971ae189180cbcf3fd37a35ae3fb183833168.zip
std: add Zero impls for &[] and &str.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/str.rs20
-rw-r--r--src/libstd/vec.rs22
2 files changed, 42 insertions, 0 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index aa6b1470b26..d8aab1a8dcc 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -2202,6 +2202,12 @@ impl<'self> Iterator<u8> for StrBytesRevIterator<'self> {
     }
 }
 
+// This works because every lifetime is a sub-lifetime of 'static
+impl<'self> Zero for &'self str {
+    fn zero() -> &'self str { "" }
+    fn is_zero(&self) -> bool { self.is_empty() }
+}
+
 impl Zero for ~str {
     fn zero() -> ~str { ~"" }
     fn is_zero(&self) -> bool { self.len() == 0 }
@@ -3317,4 +3323,18 @@ mod tests {
         t("zzz", "zz", ~["","z"]);
         t("zzzzz", "zz", ~["","","z"]);
     }
+
+    #[test]
+    fn test_str_zero() {
+        use num::Zero;
+        fn t<S: Zero + Str>() {
+            let s: S = Zero::zero();
+            assert_eq!(s.as_slice(), "");
+            assert!(s.is_zero());
+        }
+
+        t::<&str>();
+        t::<@str>();
+        t::<~str>();
+    }
 }
diff --git a/src/libstd/vec.rs b/src/libstd/vec.rs
index 1a236a49a32..cdd9d71273a 100644
--- a/src/libstd/vec.rs
+++ b/src/libstd/vec.rs
@@ -2629,6 +2629,12 @@ impl<A:Clone> Clone for ~[A] {
     }
 }
 
+// This works because every lifetime is a sub-lifetime of 'static
+impl<'self, A> Zero for &'self [A] {
+    fn zero() -> &'self [A] { &'self [] }
+    fn is_zero(&self) -> bool { self.is_empty() }
+}
+
 impl<A> Zero for ~[A] {
     fn zero() -> ~[A] { ~[] }
     fn is_zero(&self) -> bool { self.len() == 0 }
@@ -4293,4 +4299,20 @@ mod tests {
         }
         assert_eq!(v, ~[~[1,2,3],~[1,3,2],~[2,1,3],~[2,3,1],~[3,1,2],~[3,2,1]]);
     }
+
+    #[test]
+    fn test_vec_zero() {
+        use num::Zero;
+        macro_rules! t (
+            ($ty:ty) => {
+                let v: $ty = Zero::zero();
+                assert!(v.is_empty());
+                assert!(v.is_zero());
+            }
+        );
+
+        t!(&[int]);
+        t!(@[int]);
+        t!(~[int]);
+    }
 }