about summary refs log tree commit diff
path: root/src/libstd/str.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-06-16 01:52:09 -0700
committerbors <bors@rust-lang.org>2013-06-16 01:52:09 -0700
commit08c1155a223eb79960e7a5dbb0ea6276ef9754ea (patch)
treefdbb9cbe16c75d9cb615fc651a3946fb6391ed17 /src/libstd/str.rs
parentd1927d295013e19e57a9773c37ded698e89392eb (diff)
parent893c70d7bc670054ef646b71d4d503298cc50d76 (diff)
downloadrust-08c1155a223eb79960e7a5dbb0ea6276ef9754ea.tar.gz
rust-08c1155a223eb79960e7a5dbb0ea6276ef9754ea.zip
auto merge of #7142 : alexcrichton/rust/deriving-zero, r=pcwalton
This allows mass-initialization of large structs without having to specify all the fields.

I'm a bit hesitant, but I wanted to get this out there. I don't really like using the `Zero` trait, because it doesn't really make sense for a type like `HashMap` to use `Zero` as the 'blank allocation' trait. In theory there'd be a new trait, but then that's adding cruft to the language which may not necessarily need to be there.

I do think that this can be useful, but I only implemented `Zero` on the basic types where I thought it made sense, so it may not be all that usable yet. (opinions?)
Diffstat (limited to 'src/libstd/str.rs')
-rw-r--r--src/libstd/str.rs11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 21f747317f4..aa6b1470b26 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -27,6 +27,7 @@ use container::Container;
 use iter::Times;
 use iterator::{Iterator, IteratorUtil, FilterIterator, AdditiveIterator, MapIterator};
 use libc;
+use num::Zero;
 use option::{None, Option, Some};
 use old_iter::{BaseIter, EqIter};
 use ptr;
@@ -2201,6 +2202,16 @@ impl<'self> Iterator<u8> for StrBytesRevIterator<'self> {
     }
 }
 
+impl Zero for ~str {
+    fn zero() -> ~str { ~"" }
+    fn is_zero(&self) -> bool { self.len() == 0 }
+}
+
+impl Zero for @str {
+    fn zero() -> @str { @"" }
+    fn is_zero(&self) -> bool { self.len() == 0 }
+}
+
 #[cfg(test)]
 mod tests {
     use iterator::IteratorUtil;