提交 a8221bd5 编写于 作者: B bors

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

// 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;
}
......@@ -81,6 +81,7 @@
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};
......
......@@ -148,7 +148,7 @@ pub mod linkhack {
pub mod io;
pub mod hash;
pub mod container;
pub mod default;
/* Common data structures */
......
......@@ -26,7 +26,7 @@
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 unstable::raw::{Repr, Slice};
use vec;
use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector};
use default::Default;
/*
Section: Conditions
......@@ -2467,19 +2468,16 @@ fn extend<T: Iterator<char>>(&mut self, iterator: &mut T) {
}
// 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 @@ fn t<'a>(s: &str, sep: &'a str, u: ~[&str]) {
}
#[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>();
......
......@@ -52,3 +52,8 @@ fn zero() -> () { () }
#[inline]
fn is_zero(&self) -> bool { true }
}
#[cfg(not(test))]
impl Default for () {
fn default() -> () { () }
}
......@@ -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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册