Skip to content

Add a Default trait. #8438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/libstd/default.rs
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions src/libstd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub mod clone;
pub mod io;
pub mod hash;
pub mod container;

pub mod default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this file ended up getting added.


/* Common data structures */

Expand Down
27 changes: 12 additions & 15 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +35,7 @@ use uint;
use unstable::raw::{Repr, Slice};
use vec;
use vec::{OwnedVector, OwnedCopyableVector, ImmutableVector, MutableVector};
use default::Default;

/*
Section: Conditions
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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>();
Expand Down
5 changes: 5 additions & 0 deletions src/libstd/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ impl Zero for () {
#[inline]
fn is_zero(&self) -> bool { true }
}

#[cfg(not(test))]
impl Default for () {
fn default() -> () { () }
}
2 changes: 0 additions & 2 deletions src/test/run-pass/deriving-zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down