Enum attribute_derive::utils::FlagOrValue

source ·
pub enum FlagOrValue<T> {
    None,
    Flag,
    Value(T),
}
Expand description

FromAttr value that can be used both as a flag and with a value.

When parameter is specified both as flag and as value, the value will dominate.

#[derive(FromAttr)]
struct Test {
    param: FlagOrValue<String>,
}

assert_eq!(
    Test::from_args(quote!(param)).unwrap().param,
    FlagOrValue::Flag
);
assert_eq!(
    Test::from_args(quote!(param = "value")).unwrap().param,
    FlagOrValue::Value("value".into())
);
assert_eq!(
    Test::from_args(quote!(param, param = "value", param))
        .unwrap()
        .param,
    FlagOrValue::Value("value".into())
);
assert_eq!(Test::from_args(quote!()).unwrap().param, FlagOrValue::None);

Variants§

§

None

Was not specified.

§

Flag

Was specified as a flag, i.e., without a value.

§

Value(T)

Was specified with a value.

Implementations§

source§

impl<T> FlagOrValue<T>

source

pub fn is_none(&self) -> bool

Was not specified.

source

pub fn is_flag(&self) -> bool

Was specified as a flag, i.e., without a value.

source

pub fn is_value(&self) -> bool

Was specified with a value.

source

pub fn into_value(self) -> Option<T>

Returns value if set.

source

pub fn as_value(&self) -> Option<&T>

Returns value if set.

source

pub fn map_value<I>(self, map: impl FnOnce(T) -> I) -> FlagOrValue<I>

Maps the value if present.

Trait Implementations§

source§

impl<T: AttributeBase> AttributeBase for FlagOrValue<T>

§

type Partial = Partial<FlagOrValue<<T as AttributeBase>::Partial>>

Partial type for this attribute. In most cases this can be Self, unless the attribute can be parsed in multiple on-its-own-incomplete parts or needs special handling on the conversion.
source§

impl<T: AttributeValue> AttributeNamed for FlagOrValue<T>

source§

fn parse_named( name: &'static str, input: ParseStream<'_>, ) -> Result<Option<Named<SpannedValue<Self::Partial>>>>

Parses an attribute containing Self called name. Read more
source§

const PREFERRED_OPEN_DELIMITER: &'static str = " = "

What open delimiter to use when providing error messages. Read more
source§

const PREFERRED_CLOSE_DELIMITER: &'static str = ""

What close delimiter to use when providing error messages. Read more
source§

impl<T: Debug> Debug for FlagOrValue<T>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<T> Default for FlagOrValue<T>

source§

fn default() -> FlagOrValue<T>

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

impl<T: FromAttr> FromAttr for FlagOrValue<T>

source§

fn parse_partial(input: ParseStream<'_>) -> Result<Self::Partial>

Actual implementation for parsing the attribute. This is the only function required to implement in this trait and derived by the FromAttr derive macro.
source§

fn from_attribute(attr: impl Borrow<Attribute>) -> Result<Self>

Parses from a single attribute. Ignoring the name. Read more
source§

fn from_input(input: impl Into<TokenStream>) -> Result<Self>

source§

fn parse_input(input: ParseStream<'_>) -> Result<Self>

Parses input as the complete attribute. Read more
source§

fn from_attribute_partial(attr: impl Borrow<Attribute>) -> Result<Self::Partial>

Like parse_partial but instead takes an Attribute. Read more
source§

impl<T: FromPartial<P>, P> FromPartial<Partial<FlagOrValue<P>>> for FlagOrValue<T>

source§

fn from(partial: Partial<FlagOrValue<P>>) -> Result<Self>

Creates Self from T. Read more
source§

fn from_option( partial: Option<Partial<FlagOrValue<P>>>, _: &str, ) -> Result<Self>

Creates Self from optional T. Read more
source§

fn join( first: Option<SpannedValue<Partial<FlagOrValue<P>>>>, second: SpannedValue<Partial<FlagOrValue<P>>>, specified_twice_error: &str, ) -> Result<Option<SpannedValue<Partial<FlagOrValue<P>>>>>

Defines how two arguments for the same parameter should be handled. Read more
source§

impl<T: Ord> Ord for FlagOrValue<T>

source§

fn cmp(&self, other: &FlagOrValue<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<T: PartialEq> PartialEq for FlagOrValue<T>

source§

fn eq(&self, other: &FlagOrValue<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd> PartialOrd for FlagOrValue<T>

source§

fn partial_cmp(&self, other: &FlagOrValue<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T> Transpose<Option<FlagOrValue<T>>> for FlagOrValue<Option<T>>

source§

fn transpose(self) -> Option<FlagOrValue<T>>

Should behave equivalent to the built-in transpose functions available on Result<Option> and Option<Result>.
source§

impl<T, E> Transpose<Result<FlagOrValue<T>, E>> for FlagOrValue<Result<T, E>>

source§

fn transpose(self) -> Result<FlagOrValue<T>, E>

Should behave equivalent to the built-in transpose functions available on Result<Option> and Option<Result>.
source§

impl<T: Eq> Eq for FlagOrValue<T>

source§

impl<T> StructuralPartialEq for FlagOrValue<T>

Auto Trait Implementations§

§

impl<T> Freeze for FlagOrValue<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for FlagOrValue<T>
where T: RefUnwindSafe,

§

impl<T> Send for FlagOrValue<T>
where T: Send,

§

impl<T> Sync for FlagOrValue<T>
where T: Sync,

§

impl<T> Unpin for FlagOrValue<T>
where T: Unpin,

§

impl<T> UnwindSafe for FlagOrValue<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<P, T> FromPartial<Defaulting<P>> for T
where T: Default + FromPartial<P>,

source§

fn from(partial: Defaulting<P>) -> Result<T, Error>

Creates Self from T. Read more
source§

fn from_option(partial: Option<Defaulting<P>>, _error: &str) -> Result<T, Error>

Creates Self from optional T. Read more
source§

fn join( first: Option<SpannedValue<T>>, second: SpannedValue<T>, specified_twice_error: &str, ) -> Result<Option<SpannedValue<T>>>

Defines how two arguments for the same parameter should be handled. Read more
source§

impl<T> FromPartial<T> for T

source§

fn from(partial: T) -> Result<T, Error>

Creates Self from T. Read more
source§

fn from_option(partial: Option<T>, error_missing: &str) -> Result<Self>

Creates Self from optional T. Read more
source§

fn join( first: Option<SpannedValue<T>>, second: SpannedValue<T>, specified_twice_error: &str, ) -> Result<Option<SpannedValue<T>>>

Defines how two arguments for the same parameter should be handled. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.