Better Default
The std Default derive with more customization available and some upgrades.
This crate provide a single derive trait called Default
. This derive act as the std Default
derive, but allows to modify the default values of each fields. It also allows to mark enum variants with fields as default.
Features
- Does everything the std
Default
derive trait does - Support marking enum variant with fields as default
- Support overriding the default value of each fields
See all those features in actions in the Examples
chapter.
How to use
Before doing anything here, if you want to override the fields of an enum variant, you should mark it as default first
use Default;
1. Overriding the default values
There a two ways of overriding the default values : using the per-field attributes or the top default attributes.
Per-Field attributes
The per-field attributes are simply attributes you put atop of the fields for which you want to override the default values.
The syntax is the following :
<field_ident>:
You can put anything you want in the expression
bloc, as long as it can be correctly parse by syn::Expr.
Here is an example of this approach in action :
use Default;
Top Default attributes
Instead of placing an attribute on all the fields of a struct / enum variant, you place only on attribute atop of it, containing all the default values overrides.
The syntax of the top attributes is the following :
use Default;
// the struct can have unnamed fields
field_id
here can means two things : if you deal with named fields, you should put the field ident here. If you deal with unnamed fields, then you should put the position of the field (0 for the first, 1 for the second, etc.).
Again, you can put anything you want in the expression
bloc, as long as it can be correctly parse by syn::Expr.
Here are two examples, one covering unnamed fields and one covering named ones.
use Default;
use Default;
One last note : these two approaches can be combined, which means you can have a top attribute containing some default values while some of the fields have their own attribute.
Examples
- The per-field way : Usage of per-field attributes
Per field attributes are more suitable for struct / enum variants with named fields.
use Default;
use Default;
// Structs don't need to be mark as default with a top attribute. They're optional.
While not recommended, you can also use them on unnamed fields :
use Default;
// Structs don't need to be mark as default with a top attribute. They're optional.
] // set the default value of field1 to be 10
u32,
// keeps the usual default value for field2
String,
);
use Default;
- The all at once way : Usage of top default attributes
The particularity of the top attribute is that you can define all the default values at the same place.
Not all the fields need to be represented here, only those you want to modify.
use Default;
use Default;
// here we can use the top default attribute to customize the default values of our fields.
// - we change the default value of the first field (represented by the index 0) to 1
;
This can also be used on named fields :
use Default;
use Default;
Contributing
You can contribute to the project by making a pull request.
Here are the tools i use for this library :
- rustdoc-include, which allows me to import the readme directly into the
lib.rs
without copying. That's why you can see those// #[include_doc(...)]
inlib.rs
. Use thebuild_crate_doc
script in thescripts
folder to update them.
License
Licensed under Apache 2.0.