#macro-derive #default

macro sensible

Configurable derive macro for implementing Default

1 unstable release

0.1.0 May 21, 2022

#224 in #default

Download history 6/week @ 2025-08-23 21/week @ 2025-08-30 28/week @ 2025-09-06 15/week @ 2025-09-13 17/week @ 2025-09-20 12/week @ 2025-09-27 5/week @ 2025-10-04 13/week @ 2025-10-11 21/week @ 2025-10-18 27/week @ 2025-10-25 12/week @ 2025-11-01 9/week @ 2025-11-08 40/week @ 2025-11-15 57/week @ 2025-11-22 68/week @ 2025-11-29 22/week @ 2025-12-06

187 downloads per month

MIT license

7KB
83 lines

sensible

This is a crate for a more configurable derive macro to derive an implementation for Default. Rust's std::default module provides a derive implementation, but for structs, it requires that:

  • Every member on the struct implement Default.
  • Every member on the struct must use the Default implementation.

This is fine for many cases, but consider the case of a large struct where just one or two fields should default to something other than their type's Default implementation: now you have to implement the whole thing.

sensible provides a configurable derive macro that allows giving certain fields alternate default values.

Example

use sensible::Default;

#[derive(Default)]
struct Foo {
  a: u32,  // default value: 0

  #[default(42)]
  b: u32,  // default value: 42
}

Dependencies

~2MB
~48K SLoC