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>
Source§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§impl<T: PartialOrd> PartialOrd for FlagOrValue<T>
impl<T: PartialOrd> PartialOrd for FlagOrValue<T>
Source§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