Skip to content

Commit a8221bd

Browse files
committed
auto merge of #8438 : cmr/rust/default, r=thestinger
2 parents 1ac7d80 + 6eb924d commit a8221bd

File tree

6 files changed

+36
-18
lines changed

6 files changed

+36
-18
lines changed

src/libstd/default.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! The Default trait
12+
13+
/// A trait that types which have a useful default value should implement.
14+
pub trait Default {
15+
/// Return the "default value" for a type.
16+
fn default() -> Self;
17+
}

src/libstd/prelude.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ pub use vec::{Vector, VectorVector, CopyableVector, ImmutableVector};
8181
pub use vec::{ImmutableEqVector, ImmutableTotalOrdVector, ImmutableCopyableVector};
8282
pub use vec::{OwnedVector, OwnedCopyableVector,OwnedEqVector, MutableVector};
8383
pub use io::{Reader, ReaderUtil, Writer, WriterUtil};
84+
pub use default::Default;
8485

8586
// Reexported runtime types
8687
pub use comm::{stream, Port, Chan, GenericChan, GenericSmartChan, GenericPort, Peekable};

src/libstd/std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ pub mod clone;
148148
pub mod io;
149149
pub mod hash;
150150
pub mod container;
151-
151+
pub mod default;
152152

153153
/* Common data structures */
154154

src/libstd/str.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use iterator::{Iterator, FromIterator, Extendable};
2626
use iterator::{Filter, AdditiveIterator, Map};
2727
use iterator::{Invert, DoubleEndedIterator};
2828
use libc;
29-
use num::{Saturating, Zero};
29+
use num::{Saturating};
3030
use option::{None, Option, Some};
3131
use ptr;
3232
use ptr::RawPtr;
@@ -35,6 +35,7 @@ use uint;
3535
use unstable::raw::{Repr, Slice};
3636
use vec;
3737
use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector};
38+
use default::Default;
3839

3940
/*
4041
Section: Conditions
@@ -2467,19 +2468,16 @@ impl Extendable<char> for ~str {
24672468
}
24682469
24692470
// This works because every lifetime is a sub-lifetime of 'static
2470-
impl<'self> Zero for &'self str {
2471-
fn zero() -> &'self str { "" }
2472-
fn is_zero(&self) -> bool { self.is_empty() }
2471+
impl<'self> Default for &'self str {
2472+
fn default() -> &'self str { "" }
24732473
}
24742474
2475-
impl Zero for ~str {
2476-
fn zero() -> ~str { ~"" }
2477-
fn is_zero(&self) -> bool { self.len() == 0 }
2475+
impl Default for ~str {
2476+
fn default() -> ~str { ~"" }
24782477
}
24792478
2480-
impl Zero for @str {
2481-
fn zero() -> @str { @"" }
2482-
fn is_zero(&self) -> bool { self.len() == 0 }
2479+
impl Default for @str {
2480+
fn default() -> @str { @"" }
24832481
}
24842482
24852483
#[cfg(test)]
@@ -3660,12 +3658,11 @@ mod tests {
36603658
}
36613659

36623660
#[test]
3663-
fn test_str_zero() {
3664-
use num::Zero;
3665-
fn t<S: Zero + Str>() {
3666-
let s: S = Zero::zero();
3661+
fn test_str_default() {
3662+
use default::Default;
3663+
fn t<S: Default + Str>() {
3664+
let s: S = Default::default();
36673665
assert_eq!(s.as_slice(), "");
3668-
assert!(s.is_zero());
36693666
}
36703667

36713668
t::<&str>();

src/libstd/unit.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,8 @@ impl Zero for () {
5252
#[inline]
5353
fn is_zero(&self) -> bool { true }
5454
}
55+
56+
#[cfg(not(test))]
57+
impl Default for () {
58+
fn default() -> () { () }
59+
}

src/test/run-pass/deriving-zero.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ struct E { a: int, b: int }
2424

2525
#[deriving(Zero)]
2626
struct Lots {
27-
a: ~str,
28-
b: @str,
2927
c: Option<util::NonCopyable>,
3028
d: u8,
3129
e: char,

0 commit comments

Comments
 (0)