Skip to content

Commit

Permalink
Merge pull request #18 from flippette/master
Browse files Browse the repository at this point in the history
Add freestanding `default()`
  • Loading branch information
popzxc authored Apr 2, 2024
2 parents f1a287e + 978544c commit b371425
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//! Freestanding version of `std::default::Default::default()`.
/// Freestanding version of `std::default::Default::default()`.
///
/// Used to be available under `#![feature(default_free_fn)]`,
/// removed in https://github.com/rust-lang/rust/pull/113469.
///
/// # Examples
///
/// ```
/// use stdext::prelude::*;
///
/// #[derive(Default, PartialEq, Eq, Debug)]
/// struct StructWithLongName {
/// a: u32,
/// b: u32,
/// }
///
/// let s = StructWithLongName {
/// a: 12,
/// ..default() // Normally you have to do
/// // `Default::default()`,
/// // `<_>::default()` or
/// // `StructWithLongName::default()`
/// };
///
/// assert_eq!(s, StructWithLongName { a: 12, ..<_>::default() });
/// ```
pub fn default<T: Default>() -> T {
T::default()
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
pub mod duration;
#[macro_use]
pub mod macros;
pub mod default;
pub mod num;
pub mod option;
pub mod result;
Expand All @@ -142,6 +143,7 @@ pub mod vec;
/// A "prelude" module which re-exports all the extension traits for the simple library usage.
pub mod prelude {
pub use crate::{
default::*,
duration::*,
num::{float_convert::*, integer::*},
option::*,
Expand Down

0 comments on commit b371425

Please sign in to comment.