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>
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 morefn from_expr(expr: &Expr) -> Result<Self>
source§fn from_char(value: char) -> Result<Self>
fn from_char(value: char) -> Result<Self>
source§fn from_string(value: &str) -> Result<Self>
fn from_string(value: &str) -> Result<Self>
impl<T: Eq> Eq for Override<T>
impl<T> StructuralPartialEq for Override<T>
Auto Trait Implementations§
impl<T> Freeze for Override<T>where
T: Freeze,
impl<T> RefUnwindSafe for Override<T>where
T: RefUnwindSafe,
impl<T> Send for Override<T>where
T: Send,
impl<T> Sync for Override<T>where
T: Sync,
impl<T> Unpin for Override<T>where
T: Unpin,
impl<T> UnwindSafe for Override<T>where
T: UnwindSafe,
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
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)
clone_to_uninit
)