From 87d9d37c07c96ed2fe816e9cda6e3d1e14cda2fc Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Sat, 10 Aug 2013 09:38:00 -0400 Subject: [PATCH 1/2] Add a Default trait. --- src/libstd/default.rs | 17 +++++++++++++++++ src/libstd/prelude.rs | 1 + src/libstd/std.rs | 2 +- src/libstd/str.rs | 27 ++++++++++++--------------- src/libstd/unit.rs | 5 +++++ 5 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 src/libstd/default.rs 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 or the MIT license +// , 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 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() { - let s: S = Zero::zero(); + fn test_str_default() { + use default::Default; + fn t() { + 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() -> () { () } +} From 6eb924d28e0d7638a6b2d2b7c208cbaf3d1046c1 Mon Sep 17 00:00:00 2001 From: Corey Richardson Date: Mon, 26 Aug 2013 19:47:58 -0400 Subject: [PATCH 2/2] Fix deriving-zero test --- src/test/run-pass/deriving-zero.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/run-pass/deriving-zero.rs b/src/test/run-pass/deriving-zero.rs index 54895d1796f..21956ac24ee 100644 --- a/src/test/run-pass/deriving-zero.rs +++ b/src/test/run-pass/deriving-zero.rs @@ -24,8 +24,6 @@ struct E { a: int, b: int } #[deriving(Zero)] struct Lots { - a: ~str, - b: @str, c: Option, d: u8, e: char,