pub struct Flag(_);
Expand description
A meta-item that can be present as a word - with no value - or absent.
Defaulting
Like Option
, Flag
does not require #[darling(default)]
to be optional.
If the caller does not include the property, then an absent Flag
will be included
in the receiver struct.
Spans
Flag
keeps the span where its word was seen.
This enables attaching custom error messages to the word, such as in the case of two
conflicting flags being present.
Example
#[derive(FromMeta)]
#[darling(and_then = "Self::not_both")]
struct Demo {
flag_a: Flag,
flag_b: Flag,
}
impl Demo {
fn not_both(self) -> Result<Self> {
if self.flag_a.is_present() && self.flag_b.is_present() {
Err(Error::custom("Cannot set flag_a and flag_b").with_span(self.flag_b))
} else {
Ok(self)
}
}
}
The above struct would then produce the following error.
#[example(flag_a, flag_b)]
// ^^^^^^ Cannot set flag_a and flag_b
Implementations
Trait Implementations
sourceimpl FromMeta for Flag
impl FromMeta for Flag
sourcepub fn from_none() -> Option<Flag>
pub fn from_none() -> Option<Flag>
When a field is omitted from a parent meta-item, from_none
is used to attempt
recovery before a missing field error is generated. Read more
sourcepub fn from_meta(mi: &Meta) -> Result<Flag, Error>
pub fn from_meta(mi: &Meta) -> Result<Flag, Error>
Create an instance from a syn::Meta
by dispatching to the format-appropriate
trait function. This generally should not be overridden by implementers. Read more
fn from_nested_meta(item: &NestedMeta) -> Result<Self, Error>
sourcefn from_word() -> Result<Self, Error>
fn from_word() -> Result<Self, Error>
Create an instance from the presence of the word in the attribute with no additional options specified. Read more
sourcefn from_list(items: &[NestedMeta]) -> Result<Self, Error>
fn from_list(items: &[NestedMeta]) -> Result<Self, Error>
Create an instance from a list of nested meta items.
sourcefn from_value(value: &Lit) -> Result<Self, Error>
fn from_value(value: &Lit) -> Result<Self, Error>
Create an instance from a literal value of either foo = "bar"
or foo("bar")
.
This dispatches to the appropriate method based on the type of literal encountered,
and generally should not be overridden by implementers. Read more
sourcefn from_char(value: char) -> Result<Self, Error>
fn from_char(value: char) -> Result<Self, Error>
Create an instance from a char literal in a value position.
sourcefn from_string(value: &str) -> Result<Self, Error>
fn from_string(value: &str) -> Result<Self, Error>
Create an instance from a string literal in a value position.
sourceimpl Spanned for Flag
impl Spanned for Flag
sourcepub fn span(&self) -> Span
pub fn span(&self) -> Span
Returns a Span
covering the complete contents of this syntax tree
node, or Span::call_site()
if this node is empty. Read more
impl Copy for Flag
Auto Trait Implementations
impl RefUnwindSafe for Flag
impl !Send for Flag
impl !Sync for Flag
impl Unpin for Flag
impl UnwindSafe for Flag
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more