Skip to content

Commit 90b999a

Browse files
committed
auto merge of #7198 : huonw/rust/slice-zeros, r=Aatch
2 parents 98bd683 + f1d971a commit 90b999a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/libstd/str.rs

+20
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,12 @@ impl<'self> Iterator<u8> for StrBytesRevIterator<'self> {
22022202
}
22032203
}
22042204
2205+
// This works because every lifetime is a sub-lifetime of 'static
2206+
impl<'self> Zero for &'self str {
2207+
fn zero() -> &'self str { "" }
2208+
fn is_zero(&self) -> bool { self.is_empty() }
2209+
}
2210+
22052211
impl Zero for ~str {
22062212
fn zero() -> ~str { ~"" }
22072213
fn is_zero(&self) -> bool { self.len() == 0 }
@@ -3317,4 +3323,18 @@ mod tests {
33173323
t("zzz", "zz", ~["","z"]);
33183324
t("zzzzz", "zz", ~["","","z"]);
33193325
}
3326+
3327+
#[test]
3328+
fn test_str_zero() {
3329+
use num::Zero;
3330+
fn t<S: Zero + Str>() {
3331+
let s: S = Zero::zero();
3332+
assert_eq!(s.as_slice(), "");
3333+
assert!(s.is_zero());
3334+
}
3335+
3336+
t::<&str>();
3337+
t::<@str>();
3338+
t::<~str>();
3339+
}
33203340
}

src/libstd/vec.rs

+22
Original file line numberDiff line numberDiff line change
@@ -2629,6 +2629,12 @@ impl<A:Clone> Clone for ~[A] {
26292629
}
26302630
}
26312631

2632+
// This works because every lifetime is a sub-lifetime of 'static
2633+
impl<'self, A> Zero for &'self [A] {
2634+
fn zero() -> &'self [A] { &'self [] }
2635+
fn is_zero(&self) -> bool { self.is_empty() }
2636+
}
2637+
26322638
impl<A> Zero for ~[A] {
26332639
fn zero() -> ~[A] { ~[] }
26342640
fn is_zero(&self) -> bool { self.len() == 0 }
@@ -4293,4 +4299,20 @@ mod tests {
42934299
}
42944300
assert_eq!(v, ~[~[1,2,3],~[1,3,2],~[2,1,3],~[2,3,1],~[3,1,2],~[3,2,1]]);
42954301
}
4302+
4303+
#[test]
4304+
fn test_vec_zero() {
4305+
use num::Zero;
4306+
macro_rules! t (
4307+
($ty:ty) => {
4308+
let v: $ty = Zero::zero();
4309+
assert!(v.is_empty());
4310+
assert!(v.is_zero());
4311+
}
4312+
);
4313+
4314+
t!(&[int]);
4315+
t!(@[int]);
4316+
t!(~[int]);
4317+
}
42964318
}

0 commit comments

Comments
 (0)