2013-08-10 08:38:00 -05:00
|
|
|
// 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.
|
|
|
|
|
2014-09-22 15:43:47 -05:00
|
|
|
//! The `Default` trait for types which may have meaningful default values.
|
|
|
|
//!
|
|
|
|
//! Sometimes, you want to fall back to some kind of default value, and
|
|
|
|
//! don't particularly care what it is. This comes up often with `struct`s
|
|
|
|
//! that define a set of options:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! struct SomeOptions {
|
|
|
|
//! foo: int,
|
|
|
|
//! bar: f32,
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! How can we define some default values? You can use `Default`:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use std::default::Default;
|
|
|
|
//!
|
2015-01-03 21:54:18 -06:00
|
|
|
//! #[derive(Default)]
|
2014-09-22 15:43:47 -05:00
|
|
|
//! struct SomeOptions {
|
|
|
|
//! foo: int,
|
|
|
|
//! bar: f32,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let options: SomeOptions = Default::default();
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Now, you get all of the default values. Rust implements `Default` for various primitives types.
|
|
|
|
//! If you have your own type, you need to implement `Default` yourself:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use std::default::Default;
|
|
|
|
//!
|
|
|
|
//! enum Kind {
|
|
|
|
//! A,
|
|
|
|
//! B,
|
|
|
|
//! C,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! impl Default for Kind {
|
2014-11-06 02:05:53 -06:00
|
|
|
//! fn default() -> Kind { Kind::A }
|
2014-09-22 15:43:47 -05:00
|
|
|
//! }
|
|
|
|
//!
|
2015-01-03 21:54:18 -06:00
|
|
|
//! #[derive(Default)]
|
2014-09-22 15:43:47 -05:00
|
|
|
//! struct SomeOptions {
|
|
|
|
//! foo: int,
|
|
|
|
//! bar: f32,
|
|
|
|
//! baz: Kind,
|
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//!
|
|
|
|
//! fn main() {
|
|
|
|
//! let options: SomeOptions = Default::default();
|
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! If you want to override a particular option, but still retain the other defaults:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! # use std::default::Default;
|
2015-01-03 21:54:18 -06:00
|
|
|
//! # #[derive(Default)]
|
2014-09-22 15:43:47 -05:00
|
|
|
//! # struct SomeOptions {
|
|
|
|
//! # foo: int,
|
|
|
|
//! # bar: f32,
|
|
|
|
//! # }
|
|
|
|
//! fn main() {
|
|
|
|
//! let options = SomeOptions { foo: 42, ..Default::default() };
|
|
|
|
//! }
|
|
|
|
//! ```
|
2013-08-10 08:38:00 -05:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-07-18 18:01:55 -05:00
|
|
|
|
2013-08-10 08:38:00 -05:00
|
|
|
/// A trait that types which have a useful default value should implement.
|
2014-09-22 15:43:47 -05:00
|
|
|
///
|
|
|
|
/// A struct can derive default implementations of `Default` for basic types using
|
2015-01-03 21:54:18 -06:00
|
|
|
/// `#[derive(Default)]`.
|
2014-09-22 15:43:47 -05:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Default)]
|
2014-09-22 15:43:47 -05:00
|
|
|
/// struct SomeOptions {
|
|
|
|
/// foo: int,
|
|
|
|
/// bar: f32,
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-08-10 08:38:00 -05:00
|
|
|
pub trait Default {
|
2014-09-22 15:43:47 -05:00
|
|
|
/// Returns the "default value" for a type.
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
2014-09-22 15:43:47 -05:00
|
|
|
/// Default values are often some kind of initial value, identity value, or anything else that
|
|
|
|
/// may make sense as a default.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Using built-in default values:
|
2014-07-19 05:10:09 -05:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::default::Default;
|
|
|
|
///
|
|
|
|
/// let i: i8 = Default::default();
|
|
|
|
/// let (x, y): (Option<String>, f64) = Default::default();
|
|
|
|
/// let (a, b, (c, d)): (int, uint, (bool, bool)) = Default::default();
|
|
|
|
/// ```
|
2014-09-22 15:43:47 -05:00
|
|
|
///
|
|
|
|
/// Making your own:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::default::Default;
|
|
|
|
///
|
|
|
|
/// enum Kind {
|
|
|
|
/// A,
|
|
|
|
/// B,
|
|
|
|
/// C,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Default for Kind {
|
2014-11-06 02:05:53 -06:00
|
|
|
/// fn default() -> Kind { Kind::A }
|
2014-09-22 15:43:47 -05:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-08-10 08:38:00 -05:00
|
|
|
fn default() -> Self;
|
|
|
|
}
|
2013-09-11 23:49:25 -05:00
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! default_impl {
|
2014-05-26 12:33:04 -05:00
|
|
|
($t:ty, $v:expr) => {
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-26 12:33:04 -05:00
|
|
|
impl Default for $t {
|
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-26 12:33:04 -05:00
|
|
|
fn default() -> $t { $v }
|
|
|
|
}
|
|
|
|
}
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
default_impl! { (), () }
|
|
|
|
default_impl! { bool, false }
|
|
|
|
default_impl! { char, '\x00' }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2015-01-22 08:08:56 -06:00
|
|
|
default_impl! { uint, 0 }
|
|
|
|
default_impl! { u8, 0 }
|
|
|
|
default_impl! { u16, 0 }
|
|
|
|
default_impl! { u32, 0 }
|
|
|
|
default_impl! { u64, 0 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2015-01-22 08:08:56 -06:00
|
|
|
default_impl! { int, 0 }
|
|
|
|
default_impl! { i8, 0 }
|
|
|
|
default_impl! { i16, 0 }
|
|
|
|
default_impl! { i32, 0 }
|
|
|
|
default_impl! { i64, 0 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
default_impl! { f32, 0.0f32 }
|
|
|
|
default_impl! { f64, 0.0f64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|