pub struct Defs<'a> { /* private fields */ }
Expand description
Defs
is a collection of Def
s
§Example
use macro_input::{DefaultValue, Def, Defs};
const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None));
const BAZ_FIELD: Def = Def::new("foo", "baz", false, DefaultValue::Str(None));
const FOO_FIELDS: &[&Def] = &[&BAR_FIELD, &BAZ_FIELD];
const FOO_FIELD_DEFS: Defs = Defs::new(FOO_FIELDS);
Implementations§
Source§impl<'a> Defs<'a>
impl<'a> Defs<'a>
Sourcepub const fn new(defs: &'a [&'a Def<'a>]) -> Defs<'a>
pub const fn new(defs: &'a [&'a Def<'a>]) -> Defs<'a>
create a new collection of Def
s from a slice
Sourcepub fn strip(&self, attrs: &mut Vec<Attribute>)
pub fn strip(&self, attrs: &mut Vec<Attribute>)
strip away the attributes for all fields
This is useful for attribute macros because rust has no way of knowing which attributes were used.
use macro_input::{DefaultValue, Def, Defs};
use syn::{parse_quote, Attribute};
// construct some attributes
let attr1: Attribute = parse_quote!(#[foo(bar = false)]);
let attr2: Attribute = parse_quote!(#[foo(baz = "baz")]);
let attr3: Attribute = parse_quote!(#[foo(qux = "qux", quux = 3)]);
let attr4: Attribute = parse_quote!(#[foo(quuz = 'b')]);
let attr5: Attribute = parse_quote!(#[some(thing = "value")]);
let mut attrs = vec![attr1, attr2, attr3, attr4, attr5];
// strip away all mentions of the fields bar, baz and qux in foo
const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None));
const BAZ_FIELD: Def = Def::new("foo", "baz", false, DefaultValue::Str(None));
const QUX_FIELD: Def = Def::new("foo", "qux", false, DefaultValue::Str(None));
const FOO_FIELDS: &[&Def] = &[&BAR_FIELD, &BAZ_FIELD, &QUX_FIELD];
const FOO_FIELD_DEFS: Defs = Defs::new(FOO_FIELDS);
FOO_FIELD_DEFS.strip(&mut attrs);
// the Vec no longer contains
// #[foo(bar = false)], #[foo(baz = "baz")] and #[foo(qux = "qux")]
// but
// #[foo(quux = 3)], #[foo(quuz = 'b')], #[some(thing = "value")]
// remain
let attr1: Attribute = parse_quote!(#[foo(quux = 3)]);
let attr2: Attribute = parse_quote!(#[foo(quuz = 'b')]);
let attr3: Attribute = parse_quote!(#[some(thing = "value")]);
assert_eq!(attrs, vec![attr1, attr2, attr3]);
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for Defs<'a>
impl<'a> RefUnwindSafe for Defs<'a>
impl<'a> !Send for Defs<'a>
impl<'a> !Sync for Defs<'a>
impl<'a> Unpin for Defs<'a>
impl<'a> UnwindSafe for Defs<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more