pub enum Override<T> {
Inherit,
Explicit(T),
}
Expand description
A value which can inherit a default value or have an explicit value specified.
§Usage
This type is meant for attributes like default
in darling
, which can take the following forms:
#[darling(default)]
#[darling(default="path::to::fn")]
In a struct collecting input for this attribute, that would be written as:
use darling::{util::Override, FromField};
#[derive(FromField)]
#[darling(attributes(darling))]
pub struct Options {
default: Option<Override<syn::Path>>,
}
impl Options {
fn hydrate(self) -> Option<syn::Path> {
self.default.map(|ov| ov.unwrap_or(syn::parse_path("::Default::default").unwrap()))
}
}
The word
format (with no associated value), would produce Override::Inherit
, while a list
or value format would produce Override::Explicit
.
Variants§
Implementations§
Source§impl<T> Override<T>
impl<T> Override<T>
Sourcepub fn as_ref(&self) -> Override<&T>
pub fn as_ref(&self) -> Override<&T>
Converts from Override<T>
to Override<&T>
.
Produces a new Override
, containing a reference into the original, leaving the original in place.
Sourcepub fn as_mut(&mut self) -> Override<&mut T>
pub fn as_mut(&mut self) -> Override<&mut T>
Converts from Override<T>
to Override<&mut T>
.
Produces a new Override
, containing a mutable reference into the original.
Sourcepub fn is_explicit(&self) -> bool
pub fn is_explicit(&self) -> bool
Returns true
if the override is an Explicit
value.
Sourcepub fn unwrap_or(self, optb: T) -> T
pub fn unwrap_or(self, optb: T) -> T
Unwraps an override, yielding the content of an Explicit
. Otherwise, it returns optb
.
Sourcepub fn unwrap_or_else<F>(self, op: F) -> Twhere
F: FnOnce() -> T,
pub fn unwrap_or_else<F>(self, op: F) -> Twhere
F: FnOnce() -> T,
Unwraps an override, yielding the content of an Explicit
. Otherwise, it calls op
.
Trait Implementations§
Source§impl<T: FromMeta> FromMeta for Override<T>
Parses a Meta
. A bare word will produce Override::Inherit
, while
any value will be forwarded to T::from_meta
.
impl<T: FromMeta> FromMeta for Override<T>
Parses a Meta
. A bare word will produce Override::Inherit
, while
any value will be forwarded to T::from_meta
.
Source§fn from_word() -> Result<Self>
fn from_word() -> Result<Self>
Source§fn from_list(items: &[NestedMeta]) -> Result<Self>
fn from_list(items: &[NestedMeta]) -> Result<Self>
Source§fn from_value(lit: &Lit) -> Result<Self>
fn from_value(lit: &Lit) -> Result<Self>
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_nested_meta(item: &NestedMeta) -> Result<Self>
Source§fn from_meta(item: &Meta) -> Result<Self>
fn from_meta(item: &Meta) -> Result<Self>
syn::Meta
by dispatching to the format-appropriate
trait function. This generally should not be overridden by implementers. Read moreSource§fn from_none() -> Option<Self>
fn from_none() -> Option<Self>
from_none
is used to attempt
recovery before a missing field error is generated. Read more