Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add freestanding default() #18

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading