about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCorey Richardson <corey@octayn.net>2013-08-10 09:38:00 -0400
committerCorey Richardson <corey@octayn.net>2013-08-26 19:25:53 -0400
commit87d9d37c07c96ed2fe816e9cda6e3d1e14cda2fc (patch)
tree759e815c2d5fad538dc91fbacb3f3a096b1b5586
parent36b511558595a49b8c091937915d7616c2d62f14 (diff)
downloadrust-87d9d37c07c96ed2fe816e9cda6e3d1e14cda2fc.tar.gz
rust-87d9d37c07c96ed2fe816e9cda6e3d1e14cda2fc.zip
Add a Default trait.
-rw-r--r--src/libstd/default.rs17
-rw-r--r--src/libstd/prelude.rs1
-rw-r--r--src/libstd/std.rs2
-rw-r--r--src/libstd/str.rs27
-rw-r--r--src/libstd/unit.rs5
5 files changed, 36 insertions, 16 deletions
diff --git a/src/libstd/default.rs b/src/libstd/default.rs
new file mode 100644
index 00000000000..fbc60ffd01b
--- /dev/null
+++ b/src/libstd/default.rs
@@ -0,0 +1,17 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+//! The Default trait
+
+/// A trait that types which have a useful default value should implement.
+pub trait Default {
+    /// Return the "default value" for a type.
+    fn default() -> Self;
+}
diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs
index e91c78e8223..bfe5b498f8f 100644
--- a/src/libstd/prelude.rs
+++ b/src/libstd/prelude.rs
@@ -81,6 +81,7 @@ pub use vec::{Vector, VectorVector, CopyableVector, ImmutableVector};
 pub use vec::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCopyableVector};
 pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector, MutableVector};
 pub use io::{Reader, ReaderUtil, Writer, WriterUtil};
+pub use default::Default;
 
 // Reexported runtime types
 pub use comm::{stream, Port, Chan, GenericChan, GenericSmartChan, GenericPort, Peekable};
diff --git a/src/libstd/std.rs b/src/libstd/std.rs
index 278df5b170e..ad3e4368daa 100644
--- a/src/libstd/std.rs
+++ b/src/libstd/std.rs
@@ -148,7 +148,7 @@ pub mod clone;
 pub mod io;
 pub mod hash;
 pub mod container;
-
+pub mod default;
 
 /* Common data structures */
 
diff --git a/src/libstd/str.rs b/src/libstd/str.rs
index 1a2a00022f4..e6cadd04a5b 100644
--- a/src/libstd/str.rs
+++ b/src/libstd/str.rs
@@ -26,7 +26,7 @@ use iterator::{Iterator, FromIterator, Extendable};
 use iterator::{Filter, AdditiveIterator, Map};
 use iterator::{Invert, DoubleEndedIterator};
 use libc;
-use num::{Saturating, Zero};
+use num::{Saturating};
 use option::{None, Option, Some};
 use ptr;
 use ptr::RawPtr;
@@ -35,6 +35,7 @@ use uint;
 use unstable::raw::{Repr, Slice};
 use vec;
 use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector};
+use default::Default;
 
 /*
 Section: Conditions
@@ -2467,19 +2468,16 @@ impl Extendable<char> for ~str {
 }
 
 // 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<'self> Default for &'self str {
+    fn default() -> &'self str { "" }
 }
 
-impl Zero for ~str {
-    fn zero() -> ~str { ~"" }
-    fn is_zero(&self) -> bool { self.len() == 0 }
+impl Default for ~str {
+    fn default() -> ~str { ~"" }
 }
 
-impl Zero for @str {
-    fn zero() -> @str { @"" }
-    fn is_zero(&self) -> bool { self.len() == 0 }
+impl Default for @str {
+    fn default() -> @str { @"" }
 }
 
 #[cfg(test)]
@@ -3660,12 +3658,11 @@ mod tests {
     }
 
     #[test]
-    fn test_str_zero() {
-        use num::Zero;
-        fn t<S: Zero + Str>() {
-            let s: S = Zero::zero();
+    fn test_str_default() {
+        use default::Default;
+        fn t<S: Default + Str>() {
+            let s: S = Default::default();
             assert_eq!(s.as_slice(), "");
-            assert!(s.is_zero());
         }
 
         t::<&str>();
diff --git a/src/libstd/unit.rs b/src/libstd/unit.rs
index 82f14e4c8d7..3af0322df56 100644
--- a/src/libstd/unit.rs
+++ b/src/libstd/unit.rs
@@ -52,3 +52,8 @@ impl Zero for () {
     #[inline]
     fn is_zero(&self) -> bool { true }
 }
+
+#[cfg(not(test))]
+impl Default for () {
+    fn default() -> () { () }
+}