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

Converts from Override<T> to Override<&T>.

Produces a new Override, containing a reference into the original, leaving the original in place.

Converts from Override<T> to Override<&mut T>.

Produces a new Override, containing a mutable reference into the original.

Returns true if the override is an Explicit value.

Converts from Override<T> to Option<T>.

Unwraps an override, yielding the content of an Explicit. Otherwise, it returns optb.

Unwraps an override, yielding the content of an Explicit. Otherwise, it calls op.

Returns the contained value or the default value of T.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Parses a Meta. A bare word will produce Override::Inherit, while any value will be forwarded to T::from_meta.

Create an instance from the presence of the word in the attribute with no additional options specified. Read more

Create an instance from a list of nested meta items.

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

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

Create an instance from a char literal in a value position.

Create an instance from a string literal in a value position.

Create an instance from a bool literal in a value position.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.