Skip to main content

impl_default

Macro impl_default 

Source
macro_rules! impl_default {
    ($name:ident { $($field:ident),* $(,)? }) => { ... };
}
Expand description

Generate impl Default for a struct created by schema!.

Use this instead of #[derive(Default)] to automatically generate a Default implementation bounded on all field types implementing Default.

ยงExample

use vld::prelude::*;

vld::schema! {
    #[derive(Debug)]
    pub struct Config {
        pub host: String => vld::string().with_default("localhost".into()),
        pub port: Option<i64> => vld::number().int().optional(),
        pub tags: Vec<String> => vld::array(vld::string()).with_default(vec![]),
    }
}

vld::impl_default!(Config { host, port, tags });

let cfg = Config::default();
assert_eq!(cfg.host, "");       // String::default()
assert_eq!(cfg.port, None);     // Option::default()
assert!(cfg.tags.is_empty());   // Vec::default()