提交 90b999ae 编写于 作者: B bors

auto merge of #7198 : huonw/rust/slice-zeros, r=Aatch

......@@ -2202,6 +2202,12 @@ fn next(&mut self) -> Option<u8> {
}
}
// 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 @@ fn t<'a>(s: &str, sep: &'a str, u: ~[&str]) {
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>();
}
}
......@@ -2629,6 +2629,12 @@ fn clone(&self) -> ~[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 @@ fn test_permutations3() {
}
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]);
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册