[−][src]Struct macro_input::Def
a field definition
Example
use macro_input::{DefaultValue, Def}; const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None)); const BAZ_FIELD: Def = Def::new("foo", "baz", false, DefaultValue::Str(None));
Fields
path: &'a str
the path/namespace of the field
name: &'a str
the name of the field
required: bool
whether or not this field is required
default: DefaultValue
the typed default value
Implementations
impl<'a> Def<'a>
[src]
#[must_use]pub const fn new(
path: &'a str,
name: &'a str,
required: bool,
default: DefaultValue
) -> Def<'a>
[src]
path: &'a str,
name: &'a str,
required: bool,
default: DefaultValue
) -> Def<'a>
create a new field definition
pub fn strip(&self, attrs: &mut Vec<Attribute, Global>)
[src]
strip away the attributes for this field
This is useful for attribute macros because rust has no way of knowing which attributes were used.
use macro_input::{DefaultValue, Def}; use syn::{parse_quote, Attribute}; // construct some attributes let foo_attr: Attribute = parse_quote!(#[foo(bar = false)]); let other_attr: Attribute = parse_quote!(#[some(thing = "value")]); let mut attrs = vec![foo_attr, other_attr.clone()]; // strip away all mentions of the field bar in foo const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None)); BAR_FIELD.strip(&mut attrs); // the Vec no longer contains #[foo(bar = false)] but #[some(thing = "value")] remains assert_eq!(attrs, vec![other_attr]);
pub fn get_meta(&self, attrs: &[Attribute]) -> Result<Option<Meta>, Error>
[src]
try to find the meta that has the value for this field
Errors
may return the error if the field is required but not found
pub fn get_lit(&self, attrs: &[Attribute]) -> Result<Option<Lit>, Error>
[src]
try to find the literal that has the value for this field
Errors
may return the error if the field is required but not found
pub fn get<L>(&self, attrs: &[Attribute]) -> Result<Option<L>, Error> where
L: Parse,
[src]
L: Parse,
try to parse the literal that has the value for this field
Errors
may return the error if parsing fails
pub fn get_value<V>(&self, attrs: &[Attribute]) -> Result<V, Error> where
V: FromMeta,
[src]
V: FromMeta,
try to extract the value from the literal that has the value for this field
use macro_input::{DefaultValue, Def}; use syn::{parse_quote, Attribute}; let attr = parse_quote!(#[foo(bar = false)]); const BAR_FIELD: Def = Def::new("foo", "bar", false, DefaultValue::Bool(None)); let value = BAR_FIELD.get_value::<bool>(&[attr])?; assert_eq!(value, false);
Errors
may return an error if the field doesn't exist or has a value of the wrong type
Trait Implementations
Auto Trait Implementations
impl<'a> RefUnwindSafe for Def<'a>
[src]
impl<'a> !Send for Def<'a>
[src]
impl<'a> !Sync for Def<'a>
[src]
impl<'a> Unpin for Def<'a>
[src]
impl<'a> UnwindSafe for Def<'a>
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,