forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefault.rs
197 lines (187 loc) · 4.77 KB
/
default.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// 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 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:
//!
//! ```
//! # #[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 have your own type, you need to implement `Default` yourself:
//!
//! ```
//! # #![allow(dead_code)]
//! enum Kind {
//! A,
//! B,
//! C,
//! }
//!
//! impl Default for Kind {
//! fn default() -> Kind { Kind::A }
//! }
//!
//! #[derive(Default)]
//! struct SomeOptions {
//! foo: i32,
//! 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:
//!
//! ```
//! # #[allow(dead_code)]
//! # #[derive(Default)]
//! # struct SomeOptions {
//! # foo: i32,
//! # bar: f32,
//! # }
//! fn main() {
//! let options = SomeOptions { foo: 42, ..Default::default() };
//! }
//! ```
#![stable(feature = "rust1", since = "1.0.0")]
use marker::Sized;
use mem;
/// A trait for giving a type a useful default value.
///
/// A struct can derive default implementations of `Default` for basic types using
/// `#[derive(Default)]`.
///
/// # Examples
///
/// ```
/// # #[allow(dead_code)]
/// #[derive(Default)]
/// struct SomeOptions {
/// foo: i32,
/// bar: f32,
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub trait Default: Sized {
/// Returns the "default value" for a type.
///
/// 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:
///
/// ```
/// let i: i8 = Default::default();
/// let (x, y): (Option<String>, f64) = Default::default();
/// let (a, b, (c, d)): (i32, u32, (bool, bool)) = Default::default();
/// ```
///
/// Making your own:
///
/// ```
/// # #[allow(dead_code)]
/// enum Kind {
/// A,
/// B,
/// C,
/// }
///
/// impl Default for Kind {
/// fn default() -> Kind { Kind::A }
/// }
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
fn default() -> Self;
/// Replace the value with the default and return the original value.
///
/// # Examples
///
/// Seamlessly take ownership of a vector:
///
/// ```
/// #![feature(replace_default)]
/// let mut x = vec![1, 2, 3];
/// let y = x.replace_default();
/// assert!(x.is_empty()); // empty, but still usable
/// assert_eq!(y.len(), 3);
/// ```
///
/// Extract and reset all values from a map:
///
/// ```
/// #![feature(replace_default)]
/// # use std::collections::HashMap;
/// # use std::hash::Hash;
/// fn take_values<K: Eq + Hash, V: Default>(map: &mut HashMap<K, V>) -> Vec<V> {
/// map.iter_mut().map(|(_, v)| {
/// v.replace_default()
/// }).collect()
/// }
/// ```
#[inline]
#[unstable(feature = "replace_default", issue = "0")]
fn replace_default(&mut self) -> Self {
mem::replace(self, Default::default())
}
}
macro_rules! default_impl {
($t:ty, $v:expr) => {
#[stable(feature = "rust1", since = "1.0.0")]
impl Default for $t {
#[inline]
fn default() -> $t { $v }
}
}
}
default_impl! { (), () }
default_impl! { bool, false }
default_impl! { char, '\x00' }
default_impl! { usize, 0 }
default_impl! { u8, 0 }
default_impl! { u16, 0 }
default_impl! { u32, 0 }
default_impl! { u64, 0 }
default_impl! { isize, 0 }
default_impl! { i8, 0 }
default_impl! { i16, 0 }
default_impl! { i32, 0 }
default_impl! { i64, 0 }
default_impl! { f32, 0.0f32 }
default_impl! { f64, 0.0f64 }