1
Fork 0

std: add Zero impls for &[] and &str.

This commit is contained in:
Huon Wilson 2013-06-17 17:05:51 +10:00
parent d1a2360b36
commit f1d971ae18
2 changed files with 42 additions and 0 deletions

View file

@ -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]);
}
}