Skip to content

Commit b371425

Browse files
authored
Merge pull request #18 from flippette/master
Add freestanding `default()`
2 parents f1a287e + 978544c commit b371425

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/default.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! Freestanding version of `std::default::Default::default()`.
2+
3+
/// Freestanding version of `std::default::Default::default()`.
4+
///
5+
/// Used to be available under `#![feature(default_free_fn)]`,
6+
/// removed in https://github.com/rust-lang/rust/pull/113469.
7+
///
8+
/// # Examples
9+
///
10+
/// ```
11+
/// use stdext::prelude::*;
12+
///
13+
/// #[derive(Default, PartialEq, Eq, Debug)]
14+
/// struct StructWithLongName {
15+
/// a: u32,
16+
/// b: u32,
17+
/// }
18+
///
19+
/// let s = StructWithLongName {
20+
/// a: 12,
21+
/// ..default() // Normally you have to do
22+
/// // `Default::default()`,
23+
/// // `<_>::default()` or
24+
/// // `StructWithLongName::default()`
25+
/// };
26+
///
27+
/// assert_eq!(s, StructWithLongName { a: 12, ..<_>::default() });
28+
/// ```
29+
pub fn default<T: Default>() -> T {
30+
T::default()
31+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
pub mod duration;
133133
#[macro_use]
134134
pub mod macros;
135+
pub mod default;
135136
pub mod num;
136137
pub mod option;
137138
pub mod result;
@@ -142,6 +143,7 @@ pub mod vec;
142143
/// A "prelude" module which re-exports all the extension traits for the simple library usage.
143144
pub mod prelude {
144145
pub use crate::{
146+
default::*,
145147
duration::*,
146148
num::{float_convert::*, integer::*},
147149
option::*,

0 commit comments

Comments
 (0)