pub struct Flag(/* private fields */);
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.span()))
} 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§
source§impl FromMeta for Flag
impl FromMeta for Flag
source§fn from_none() -> Option<Self>
fn from_none() -> Option<Self>
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 moresource§fn from_meta(mi: &Meta) -> Result<Self>
fn from_meta(mi: &Meta) -> Result<Self>
Create an instance from a
syn::Meta
by dispatching to the format-appropriate
trait function. This generally should not be overridden by implementers. Read morefn from_nested_meta(item: &NestedMeta) -> Result<Self>
source§fn from_word() -> Result<Self>
fn from_word() -> Result<Self>
Create an instance from the presence of the word in the attribute with no
additional options specified.
source§fn from_list(items: &[NestedMeta]) -> Result<Self>
fn from_list(items: &[NestedMeta]) -> Result<Self>
Create an instance from a list of nested meta items.
source§fn from_value(value: &Lit) -> Result<Self>
fn from_value(value: &Lit) -> Result<Self>
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 morefn from_expr(expr: &Expr) -> Result<Self>
source§fn from_char(value: char) -> Result<Self>
fn from_char(value: char) -> Result<Self>
Create an instance from a char literal in a value position.
source§fn from_string(value: &str) -> Result<Self>
fn from_string(value: &str) -> Result<Self>
Create an instance from a string literal in a value position.
impl Copy for Flag
Auto Trait Implementations§
impl Freeze for Flag
impl RefUnwindSafe for Flag
impl !Send for Flag
impl !Sync for Flag
impl Unpin for Flag
impl UnwindSafe for Flag
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
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)