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;
#[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
Inherit
Inherit the eventual value from an external source.
Explicit(T)
Explicitly set the value.
Implementations
sourceimpl<T> Override<T>
impl<T> Override<T>
sourcepub fn as_ref(&'a self) -> Override<&'a T>
pub fn as_ref(&'a self) -> Override<&'a 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(&'a mut self) -> Override<&'a mut T>
pub fn as_mut(&'a mut self) -> Override<&'a 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) -> T where
F: FnOnce() -> T,
pub fn unwrap_or_else<F>(self, op: F) -> T where
F: FnOnce() -> T,
Unwraps an override, yielding the content of an Explicit
. Otherwise, it calls op
.
sourceimpl<T> Override<T> where
T: Default,
impl<T> Override<T> where
T: Default,
sourcepub fn unwrap_or_default(self) -> T
pub fn unwrap_or_default(self) -> T
Returns the contained value or the default value of T
.
Trait Implementations
sourceimpl<T> FromMeta for Override<T> where
T: FromMeta,
impl<T> FromMeta for Override<T> where
T: FromMeta,
Parses a Meta
. A bare word will produce Override::Inherit
, while
any value will be forwarded to T::from_meta
.
sourcefn from_word() -> Result<Override<T>, Error>
fn from_word() -> Result<Override<T>, 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<Override<T>, Error>
fn from_list(items: &[NestedMeta]) -> Result<Override<T>, Error>
Create an instance from a list of nested meta items.
sourcefn from_value(lit: &Lit) -> Result<Override<T>, Error>
fn from_value(lit: &Lit) -> Result<Override<T>, 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
fn from_nested_meta(item: &NestedMeta) -> Result<Self, Error>
sourcefn from_meta(item: &Meta) -> Result<Self, Error>
fn from_meta(item: &Meta) -> Result<Self, 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
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.
impl<T> Eq for Override<T> where
T: Eq,
impl<T> StructuralEq for Override<T>
impl<T> StructuralPartialEq for Override<T>
Auto Trait Implementations
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
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.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more