| Crates.io | fancy-default |
| lib.rs | fancy-default |
| version | 0.1.0 |
| created_at | 2024-04-21 01:56:15.526177+00 |
| updated_at | 2024-04-21 01:56:15.526177+00 |
| description | A better `derive(Default)` implementation. |
| homepage | https://github.com/Embers-of-the-Fire/better-default-rs |
| repository | https://github.com/Embers-of-the-Fire/better-default-rs.git |
| max_upload_size | |
| id | 1215094 |
| size | 11,239 |
fancy-defaultThis library provides enhancements to the standard library's Default derive macro.
Default macroConstDefault macroVariantDefault macroAll default value configurations in this library use the same syntax.
#[default]: Calls core::default::Default and uses it as the default value.
Note: Currently core::default::Default is not a constant trait,
so a default value must be specified when using ConstDefault.
#[default = <expr>]: Use <expr> as the default value for this field.
Note: better_default does not use string literals to parse expressions,
so you can write expressions with default values directly,
like: #[default = "foobar".to_owned()].
#[default(expr = <expr>)]: Same meaning as the previous format.
#[default]: Set the variant as the default variant of the enum.#[default].fancy_default::derive::DefaultBasic Usage:
use fancy_default::Default;
#[derive(Debug, Default, PartialEq, Eq)]
struct Person {
#[default(expr = "no-name".to_owned())]
name: String,
#[default]
id: usize,
#[default(expr = Some("unknown".to_owned()))]
tag: Option<String>,
}
assert_eq!(
Person::default(),
Person {
name: "no-name".to_owned(),
id: 0,
tag: Some("unknown".to_owned()),
}
);
fancy_default::derive::ConstDefaultBasic Usage:
// this imports both `fancy_default::derive::ConstDefault`
// and `fancy_default::traits::ConstDefault`.
use fancy_default::ConstDefault;
#[derive(Debug, ConstDefault, PartialEq, Eq)]
struct Person<'a> {
#[default = "no-name"]
name: &'a str,
#[default = 0]
id: usize,
#[default(expr = Some("unknown"))]
tag: Option<&'a str>,
}
assert_eq!(
Person::DEFAULT,
Person {
name: "no-name",
id: 0,
tag: Some("unknown"),
}
);
fancy_default::derive::VariantDefaultSet default values for each variant of the enumeration.This derive macro uses an additional attribute variant, to set how the default value are generated.
Config Syntax:
#[variant(<config>)]:
const/const = <bool>: Whether to generate constant default values.
The corresponding constant name is the UPPER_CASE version of the current enumeration.false.
Alias: constant.func/func = <bool>: Whether to generate static methods that return default values.
The corresponding constant name is the snake_case version of the current enumeration and has a default_ prefix.true.
Alias: fn, function.Note: This attribute can be added to an enum body or to a single variant. If added to the enum body, it will override the default generated configuration.
Basic Usage:
use fancy_default::VariantDefault;
#[derive(Debug, VariantDefault, PartialEq, Eq)]
#[variant(const)]
enum Enum {
Plain,
#[variant(const = false)]
Struct {
#[default(expr = "123".to_owned())]
name: String,
},
Tuple(#[default = 10] usize),
}
assert_eq!(Enum::PLAIN, Enum::Plain);
assert_eq!(
Enum::default_struct(),
Enum::Struct {
name: "123".to_owned()
}
);
The theoretical minimum rust version of this derived macro is 1.34,
which allows passing TokenStream to MetaList from that version onwards.
This library is licensed under the MIT license or the Apache v2.0 license.