serde-const-default
Use const expressions as Serde defaults.
Install
cargo add serde-const-default
Or, add it to Cargo.toml:
[]
= "0.1"
Usage & Examples
use Deserialize;
use serde_const_default;
const SOME_VALUE: u8 = 7;
const DEFAULT_NAME: &str = "anonymous";
#[const_default = EXPR] generates a Serde default const fn that returns
EXPR directly:
const
Use this form when EXPR is valid inside a const fn.
#[const_default_from(EXPR)] generates a Serde default function that converts
the expression into the field type with From::from:
Use const_default_from when the expression is not already the field type, for
example a &'static str default for a String field.
Lazy defaults should be accessed explicitly:
use Deserialize;
use LazyLock;
use serde_const_default;
static DEFAULT_NAME: = new;
The macro supports named structs. A field cannot use both const_default and
Serde's own #[serde(default)] attribute.