2013-08-10 09:38:00 -04: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 16:43:47 -04:00
|
|
|
//! The `Default` trait for types which may have meaningful default values.
|
2013-08-10 09:38:00 -04:00
|
|
|
|
2015-01-23 21:48:20 -08:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2014-07-18 16:01:55 -07:00
|
|
|
|
2016-05-23 12:52:38 -04:00
|
|
|
/// A trait for giving a type a useful default value.
|
|
|
|
///
|
2016-05-23 13:47:28 -04:00
|
|
|
/// 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:
|
2014-09-22 16:43:47 -04:00
|
|
|
///
|
2016-05-23 13:47:28 -04:00
|
|
|
/// ```
|
|
|
|
/// # #[allow(dead_code)]
|
|
|
|
/// struct SomeOptions {
|
|
|
|
/// foo: i32,
|
|
|
|
/// bar: f32,
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// How can we define some default values? You can use `Default`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #[allow(dead_code)]
|
|
|
|
/// #[derive(Default)]
|
|
|
|
/// struct SomeOptions {
|
|
|
|
/// foo: i32,
|
|
|
|
/// 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 want to override a particular option, but still retain the other defaults:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #[allow(dead_code)]
|
|
|
|
/// # #[derive(Default)]
|
|
|
|
/// # struct SomeOptions {
|
|
|
|
/// # foo: i32,
|
|
|
|
/// # bar: f32,
|
|
|
|
/// # }
|
|
|
|
/// fn main() {
|
|
|
|
/// let options = SomeOptions { foo: 42, ..Default::default() };
|
|
|
|
/// }
|
|
|
|
/// ```
|
2016-05-22 19:24:58 -04:00
|
|
|
///
|
|
|
|
/// ## Derivable
|
|
|
|
///
|
|
|
|
/// This trait can be used with `#[derive]` if all of the type's fields implement
|
|
|
|
/// `Default`. When `derive`d, it will use the default value for each field's type.
|
|
|
|
///
|
|
|
|
/// ## How can I implement `Default`?
|
|
|
|
///
|
|
|
|
/// Provide an implementation for the `default()` method that returns the value of
|
|
|
|
/// your type that should be the default:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # #![allow(dead_code)]
|
|
|
|
/// enum Kind {
|
|
|
|
/// A,
|
|
|
|
/// B,
|
|
|
|
/// C,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Default for Kind {
|
|
|
|
/// fn default() -> Kind { Kind::A }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-09-22 16:43:47 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-11-03 15:27:03 +00:00
|
|
|
/// # #[allow(dead_code)]
|
2015-01-03 22:54:18 -05:00
|
|
|
/// #[derive(Default)]
|
2014-09-22 16:43:47 -04:00
|
|
|
/// struct SomeOptions {
|
2015-02-18 14:41:13 +01:00
|
|
|
/// foo: i32,
|
2014-09-22 16:43:47 -04:00
|
|
|
/// bar: f32,
|
|
|
|
/// }
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-10-23 21:51:38 -04:00
|
|
|
pub trait Default: Sized {
|
2014-09-22 16:43:47 -04:00
|
|
|
/// Returns the "default value" for a type.
|
2014-07-19 12:10:09 +02:00
|
|
|
///
|
2014-09-22 16:43:47 -04: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 12:10:09 +02:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let i: i8 = Default::default();
|
|
|
|
/// let (x, y): (Option<String>, f64) = Default::default();
|
2015-02-18 14:41:13 +01:00
|
|
|
/// let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();
|
2014-07-19 12:10:09 +02:00
|
|
|
/// ```
|
2014-09-22 16:43:47 -04:00
|
|
|
///
|
|
|
|
/// Making your own:
|
|
|
|
///
|
|
|
|
/// ```
|
2015-11-03 15:27:03 +00:00
|
|
|
/// # #[allow(dead_code)]
|
2014-09-22 16:43:47 -04:00
|
|
|
/// enum Kind {
|
|
|
|
/// A,
|
|
|
|
/// B,
|
|
|
|
/// C,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Default for Kind {
|
2014-11-06 00:05:53 -08:00
|
|
|
/// fn default() -> Kind { Kind::A }
|
2014-09-22 16:43:47 -04:00
|
|
|
/// }
|
|
|
|
/// ```
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-08-10 09:38:00 -04:00
|
|
|
fn default() -> Self;
|
|
|
|
}
|
2013-09-11 21:49:25 -07:00
|
|
|
|
2014-11-14 09:18:10 -08:00
|
|
|
macro_rules! default_impl {
|
2017-07-18 01:49:40 +08:00
|
|
|
($t:ty, $v:expr, $doc:tt) => {
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-05-26 19:33:04 +02:00
|
|
|
impl Default for $t {
|
|
|
|
#[inline]
|
2017-07-15 15:35:03 +02:00
|
|
|
#[doc = $doc]
|
2014-05-26 19:33:04 +02:00
|
|
|
fn default() -> $t { $v }
|
|
|
|
}
|
|
|
|
}
|
2014-11-14 09:18:10 -08:00
|
|
|
}
|
|
|
|
|
2017-07-15 17:34:37 +02:00
|
|
|
default_impl! { (), (), "Returns the default value of `()`" }
|
|
|
|
default_impl! { bool, false, "Returns the default value of `false`" }
|
|
|
|
default_impl! { char, '\x00', "Returns the default value of `\\x00`" }
|
2014-05-26 19:33:04 +02:00
|
|
|
|
2017-07-15 17:34:37 +02:00
|
|
|
default_impl! { usize, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { u8, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { u16, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { u32, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { u64, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { u128, 0, "Returns the default value of `0`" }
|
2014-05-26 19:33:04 +02:00
|
|
|
|
2017-07-15 17:34:37 +02:00
|
|
|
default_impl! { isize, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { i8, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { i16, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { i32, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { i64, 0, "Returns the default value of `0`" }
|
|
|
|
default_impl! { i128, 0, "Returns the default value of `0`" }
|
2014-05-26 19:33:04 +02:00
|
|
|
|
2017-07-15 17:34:37 +02:00
|
|
|
default_impl! { f32, 0.0f32, "Returns the default value of `0.0`" }
|
|
|
|
default_impl! { f64, 0.0f64, "Returns the default value of `0.0`" }
|