1
Fork 0

Add a Default trait.

This commit is contained in:
Corey Richardson 2013-08-10 09:38:00 -04:00
parent 36b5115585
commit 87d9d37c07
5 changed files with 36 additions and 16 deletions

17
src/libstd/default.rs Normal file
View file

@ -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;
}

View file

@ -81,6 +81,7 @@ pub use vec::{Vector, VectorVector, CopyableVector, ImmutableVector};
pub use vec::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCopyableVector}; pub use vec::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCopyableVector};
pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector, MutableVector}; pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector, MutableVector};
pub use io::{Reader, ReaderUtil, Writer, WriterUtil}; pub use io::{Reader, ReaderUtil, Writer, WriterUtil};
pub use default::Default;
// Reexported runtime types // Reexported runtime types
pub use comm::{stream, Port, Chan, GenericChan, GenericSmartChan, GenericPort, Peekable}; pub use comm::{stream, Port, Chan, GenericChan, GenericSmartChan, GenericPort, Peekable};

View file

@ -148,7 +148,7 @@ pub mod clone;
pub mod io; pub mod io;
pub mod hash; pub mod hash;
pub mod container; pub mod container;
pub mod default;
/* Common data structures */ /* Common data structures */

View file

@ -26,7 +26,7 @@ use iterator::{Iterator, FromIterator, Extendable};
use iterator::{Filter, AdditiveIterator, Map}; use iterator::{Filter, AdditiveIterator, Map};
use iterator::{Invert, DoubleEndedIterator}; use iterator::{Invert, DoubleEndedIterator};
use libc; use libc;
use num::{Saturating, Zero}; use num::{Saturating};
use option::{None, Option, Some}; use option::{None, Option, Some};
use ptr; use ptr;
use ptr::RawPtr; use ptr::RawPtr;
@ -35,6 +35,7 @@ use uint;
use unstable::raw::{Repr, Slice}; use unstable::raw::{Repr, Slice};
use vec; use vec;
use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector}; use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector};
use default::Default;
/* /*
Section: Conditions Section: Conditions
@ -2467,19 +2468,16 @@ impl Extendable<char> for ~str {
} }
// This works because every lifetime is a sub-lifetime of 'static // This works because every lifetime is a sub-lifetime of 'static
impl<'self> Zero for &'self str { impl<'self> Default for &'self str {
fn zero() -> &'self str { "" } fn default() -> &'self str { "" }
fn is_zero(&self) -> bool { self.is_empty() }
} }
impl Zero for ~str { impl Default for ~str {
fn zero() -> ~str { ~"" } fn default() -> ~str { ~"" }
fn is_zero(&self) -> bool { self.len() == 0 }
} }
impl Zero for @str { impl Default for @str {
fn zero() -> @str { @"" } fn default() -> @str { @"" }
fn is_zero(&self) -> bool { self.len() == 0 }
} }
#[cfg(test)] #[cfg(test)]
@ -3660,12 +3658,11 @@ mod tests {
} }
#[test] #[test]
fn test_str_zero() { fn test_str_default() {
use num::Zero; use default::Default;
fn t<S: Zero + Str>() { fn t<S: Default + Str>() {
let s: S = Zero::zero(); let s: S = Default::default();
assert_eq!(s.as_slice(), ""); assert_eq!(s.as_slice(), "");
assert!(s.is_zero());
} }
t::<&str>(); t::<&str>();

View file

@ -52,3 +52,8 @@ impl Zero for () {
#[inline] #[inline]
fn is_zero(&self) -> bool { true } fn is_zero(&self) -> bool { true }
} }
#[cfg(not(test))]
impl Default for () {
fn default() -> () { () }
}