[][src]Struct macro_input::Defs

pub struct Defs<'a> { /* fields omitted */ }

Defs is a collection of Defs

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

impl<'a> Defs<'a>[src]

#[must_use]pub const fn new(defs: &'a [&'a Def<'a>]) -> Defs<'a>[src]

create a new collection of Defs from a slice

#[must_use]pub const fn empty() -> &'static Defs<'static>[src]

return an empty collection of Defs

pub fn strip(&self, attrs: &mut Vec<Attribute, Global>)[src]

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

impl<'a> From<&'a [&'a Def<'a>]> for Defs<'a>[src]

impl<'_> Lint<Vec<Attribute, Global>> for Defs<'_>[src]

Auto Trait Implementations

impl<'a> RefUnwindSafe for Defs<'a>[src]

impl<'a> !Send for Defs<'a>[src]

impl<'a> !Sync for Defs<'a>[src]

impl<'a> Unpin for Defs<'a>[src]

impl<'a> UnwindSafe for Defs<'a>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.