auto merge of #8438 : cmr/rust/default, r=thestinger

This commit is contained in:
bors 2013-08-26 17:05:57 -07:00
commit a8221bd5e2
6 changed files with 36 additions and 18 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::{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};

View File

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

View File

@ -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>();

View File

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

View File

@ -24,8 +24,6 @@ struct E { a: int, b: int }
#[deriving(Zero)]
struct Lots {
a: ~str,
b: @str,
c: Option<util::NonCopyable>,
d: u8,
e: char,