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>
impl<T> FlagOrValue<T>
sourcepub fn into_value(self) -> Option<T>
pub fn into_value(self) -> Option<T>
Returns value if set.
sourcepub fn map_value<I>(self, map: impl FnOnce(T) -> I) -> FlagOrValue<I>
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>
impl<T: AttributeBase> AttributeBase for FlagOrValue<T>
§type Partial = Partial<FlagOrValue<<T as AttributeBase>::Partial>>
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>
impl<T: AttributeValue> AttributeNamed for FlagOrValue<T>
source§fn parse_named(
name: &'static str,
input: ParseStream<'_>,
) -> Result<Option<Named<SpannedValue<Self::Partial>>>>
fn parse_named( name: &'static str, input: ParseStream<'_>, ) -> Result<Option<Named<SpannedValue<Self::Partial>>>>
source§const PREFERRED_OPEN_DELIMITER: &'static str = " = "
const PREFERRED_OPEN_DELIMITER: &'static str = " = "
What open delimiter to use when providing error messages. Read more
source§const PREFERRED_CLOSE_DELIMITER: &'static str = ""
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>
impl<T: Debug> Debug for FlagOrValue<T>
source§impl<T> Default for FlagOrValue<T>
impl<T> Default for FlagOrValue<T>
source§fn default() -> FlagOrValue<T>
fn default() -> FlagOrValue<T>
Returns the “default value” for a type. Read more
source§impl<T: FromAttr> FromAttr for FlagOrValue<T>
impl<T: FromAttr> FromAttr for FlagOrValue<T>
source§fn parse_partial(input: ParseStream<'_>) -> Result<Self::Partial>
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>
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>
fn from_input(input: impl Into<TokenStream>) -> Result<Self>
Parses a
TokenStream
. Read moresource§fn parse_input(input: ParseStream<'_>) -> Result<Self>
fn parse_input(input: ParseStream<'_>) -> Result<Self>
Parses input as the complete attribute. Read more
source§impl<T: FromPartial<P>, P> FromPartial<Partial<FlagOrValue<P>>> for FlagOrValue<T>
impl<T: FromPartial<P>, P> FromPartial<Partial<FlagOrValue<P>>> for FlagOrValue<T>
source§fn from_option(
partial: Option<Partial<FlagOrValue<P>>>,
_: &str,
) -> Result<Self>
fn from_option( partial: Option<Partial<FlagOrValue<P>>>, _: &str, ) -> Result<Self>
source§fn join(
first: Option<SpannedValue<Partial<FlagOrValue<P>>>>,
second: SpannedValue<Partial<FlagOrValue<P>>>,
specified_twice_error: &str,
) -> Result<Option<SpannedValue<Partial<FlagOrValue<P>>>>>
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>
impl<T: Ord> Ord for FlagOrValue<T>
source§fn cmp(&self, other: &FlagOrValue<T>) -> Ordering
fn cmp(&self, other: &FlagOrValue<T>) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
source§impl<T: PartialEq> PartialEq for FlagOrValue<T>
impl<T: PartialEq> PartialEq for FlagOrValue<T>
source§fn eq(&self, other: &FlagOrValue<T>) -> bool
fn eq(&self, other: &FlagOrValue<T>) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl<T: PartialOrd> PartialOrd for FlagOrValue<T>
impl<T: PartialOrd> PartialOrd for FlagOrValue<T>
source§fn partial_cmp(&self, other: &FlagOrValue<T>) -> Option<Ordering>
fn partial_cmp(&self, other: &FlagOrValue<T>) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
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 moresource§impl<T> Transpose<Option<FlagOrValue<T>>> for FlagOrValue<Option<T>>
impl<T> Transpose<Option<FlagOrValue<T>>> for FlagOrValue<Option<T>>
source§fn transpose(self) -> Option<FlagOrValue<T>>
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>>
impl<T, E> Transpose<Result<FlagOrValue<T>, E>> for FlagOrValue<Result<T, E>>
source§fn transpose(self) -> Result<FlagOrValue<T>, E>
fn transpose(self) -> Result<FlagOrValue<T>, E>
Should behave equivalent to the built-in
transpose
functions available
on Result<Option>
and
Option<Result>
.impl<T: Eq> Eq for FlagOrValue<T>
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> 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
Mutably borrows from an owned value. Read more
source§impl<P, T> FromPartial<Defaulting<P>> for Twhere
T: Default + FromPartial<P>,
impl<P, T> FromPartial<Defaulting<P>> for Twhere
T: Default + FromPartial<P>,
source§fn from_option(partial: Option<Defaulting<P>>, _error: &str) -> Result<T, Error>
fn from_option(partial: Option<Defaulting<P>>, _error: &str) -> Result<T, Error>
source§fn join(
first: Option<SpannedValue<T>>,
second: SpannedValue<T>,
specified_twice_error: &str,
) -> Result<Option<SpannedValue<T>>>
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
impl<T> FromPartial<T> for T
source§fn join(
first: Option<SpannedValue<T>>,
second: SpannedValue<T>,
specified_twice_error: &str,
) -> Result<Option<SpannedValue<T>>>
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