2 releases

0.1.1 Apr 8, 2023
0.1.0 Apr 7, 2023

#1759 in Procedural macros

MIT license

9KB
149 lines

better-default-derive crates.io github actions badge

A better std::default::Default macro.

Supports:

  • Struct
    • Unit
    • Named
    • Unnamed
    • Generic
  • Enum
    • Unit
    • Named
    • Unnamed
    • Generic

Usage

[dependencies]
better-default-derive = "0.1.0"
use better_default_derive::Default;

#[derive(Debug, PartialEq, Eq, Default)]
enum Either<L, R> {
    Left(L),
    #[default]
    Right(R),
}

fn main() {
    let either: Either<String, u8> = Either::default();
    assert_eq!(either, Either::Right(u8::default()));
}

Roadmap

  • Better error message when a field is missing the Default instance.

  • Support union types

  • Support associated types

    use better_default_derive::Default;
    
    pub trait Trait {
        type Value;
    }
    
    #[derive(Default)]
    pub struct Field<T: Trait> {
        values: Vec<T::Value>,
    }
    

Dependencies

~225–690KB
~16K SLoC