Skip to content

Commit 893c70d

Browse files
committed
Add Zero impls for lots of common types
1 parent 07f5ab1 commit 893c70d

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed

src/libstd/char.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use u32;
1717
use uint;
1818
use unicode::{derived_property, general_category};
1919

20-
#[cfg(not(test))]
21-
use cmp::{Eq, Ord};
20+
#[cfg(not(test))] use cmp::{Eq, Ord};
21+
#[cfg(not(test))] use num::Zero;
2222

2323
/*
2424
Lu Uppercase_Letter an uppercase letter
@@ -328,6 +328,12 @@ impl Ord for char {
328328
fn ge(&self, other: &char) -> bool { *self >= *other }
329329
}
330330

331+
#[cfg(not(test))]
332+
impl Zero for char {
333+
fn zero() -> char { 0 as char }
334+
fn is_zero(&self) -> bool { *self == 0 as char }
335+
}
336+
331337
#[test]
332338
fn test_is_lowercase() {
333339
assert!('a'.is_lowercase());

src/libstd/num/num.rs

+15
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,21 @@ pub fn pow_with_uint<T:NumCast+One+Zero+Copy+Div<T,T>+Mul<T,T>>(radix: uint, pow
418418
total
419419
}
420420

421+
impl<T: Zero> Zero for @mut T {
422+
fn zero() -> @mut T { @mut Zero::zero() }
423+
fn is_zero(&self) -> bool { (**self).is_zero() }
424+
}
425+
426+
impl<T: Zero> Zero for @T {
427+
fn zero() -> @T { @Zero::zero() }
428+
fn is_zero(&self) -> bool { (**self).is_zero() }
429+
}
430+
431+
impl<T: Zero> Zero for ~T {
432+
fn zero() -> ~T { ~Zero::zero() }
433+
fn is_zero(&self) -> bool { (**self).is_zero() }
434+
}
435+
421436
/// Helper function for testing numeric operations
422437
#[cfg(test)]
423438
pub fn test_num<T:Num + NumCast>(ten: T, two: T) {

src/libstd/option.rs

+5
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ impl<T:Copy + Zero> Option<T> {
350350
}
351351
}
352352

353+
impl<T> Zero for Option<T> {
354+
fn zero() -> Option<T> { None }
355+
fn is_zero(&self) -> bool { self.is_none() }
356+
}
357+
353358
/// Immutable iterator over an `Option<A>`
354359
pub struct OptionIterator<'self, A> {
355360
priv opt: Option<&'self A>

src/libstd/str.rs

+11
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use container::Container;
2828
use iter::Times;
2929
use iterator::{Iterator, IteratorUtil, FilterIterator, AdditiveIterator};
3030
use libc;
31+
use num::Zero;
3132
use option::{None, Option, Some};
3233
use old_iter::{BaseIter, EqIter};
3334
use ptr;
@@ -2250,6 +2251,16 @@ impl<'self> Iterator<u8> for StrBytesRevIterator<'self> {
22502251
}
22512252
}
22522253
2254+
impl Zero for ~str {
2255+
fn zero() -> ~str { ~"" }
2256+
fn is_zero(&self) -> bool { self.len() == 0 }
2257+
}
2258+
2259+
impl Zero for @str {
2260+
fn zero() -> @str { @"" }
2261+
fn is_zero(&self) -> bool { self.len() == 0 }
2262+
}
2263+
22532264
#[cfg(test)]
22542265
mod tests {
22552266
use iterator::IteratorUtil;

src/libstd/tuple.rs

+13
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ macro_rules! tuple_impls {
135135
pub mod inner {
136136
use clone::Clone;
137137
#[cfg(not(test))] use cmp::*;
138+
#[cfg(not(test))] use num::Zero;
138139

139140
$(
140141
pub trait $cloneable_trait<$($T),+> {
@@ -210,6 +211,18 @@ macro_rules! tuple_impls {
210211
lexical_cmp!($(self.$get_ref_fn(), other.$get_ref_fn()),+)
211212
}
212213
}
214+
215+
#[cfg(not(test))]
216+
impl<$($T:Zero),+> Zero for ($($T),+) {
217+
#[inline]
218+
fn zero() -> ($($T),+) {
219+
($(Zero::zero::<$T>()),+)
220+
}
221+
#[inline]
222+
fn is_zero(&self) -> bool {
223+
$(self.$get_ref_fn().is_zero())&&+
224+
}
225+
}
213226
)+
214227
}
215228
}

src/libstd/vec.rs

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use iterator::{Iterator, IteratorUtil};
2323
use iter::FromIter;
2424
use kinds::Copy;
2525
use libc;
26+
use num::Zero;
2627
use old_iter::CopyableIter;
2728
use option::{None, Option, Some};
2829
use ptr::to_unsafe_ptr;
@@ -2702,6 +2703,16 @@ impl<A:Clone> Clone for ~[A] {
27022703
}
27032704
}
27042705

2706+
impl<A> Zero for ~[A] {
2707+
fn zero() -> ~[A] { ~[] }
2708+
fn is_zero(&self) -> bool { self.len() == 0 }
2709+
}
2710+
2711+
impl<A> Zero for @[A] {
2712+
fn zero() -> @[A] { @[] }
2713+
fn is_zero(&self) -> bool { self.len() == 0 }
2714+
}
2715+
27052716
macro_rules! iterator {
27062717
/* FIXME: #4375 Cannot attach documentation/attributes to a macro generated struct.
27072718
(struct $name:ident -> $ptr:ty, $elem:ty) => {

0 commit comments

Comments
 (0)