pub struct AttributePropertyBoolean<Marker = AttributePropertyBooleanMarker>(/* private fields */);Expand description
A generic boolean attribute property.
Defaults to false.
§Example
use macro_tools::AttributePropertyBoolean;
#[ derive( Debug, Default, Clone, Copy ) ]
pub struct DebugMarker;
#[ derive( Debug, Default, Clone, Copy ) ]
pub struct EnabledMarker;
pub trait AttributePropertyComponent
{
const KEYWORD : &'static str;
}
impl AttributePropertyComponent for DebugMarker
{
const KEYWORD : &'static str = "debug";
}
impl AttributePropertyComponent for EnabledMarker
{
const KEYWORD : &'static str = "enabled";
}
#[ derive( Debug, Default ) ]
struct MyAttributes
{
pub debug : AttributePropertyBoolean< DebugMarker >,
pub enabled : AttributePropertyBoolean< EnabledMarker >,
}
impl syn::parse::Parse for MyAttributes
{
fn parse( input : syn::parse::ParseStream< '_ > ) -> syn::Result< Self >
{
let mut debug = AttributePropertyBoolean::< DebugMarker >::default();
let mut enabled = AttributePropertyBoolean::< EnabledMarker >::default();
while !input.is_empty()
{
let lookahead = input.lookahead1();
if lookahead.peek( syn::Ident )
{
let ident : syn::Ident = input.parse()?;
match ident.to_string().as_str()
{
DebugMarker::KEYWORD => debug = input.parse()?,
EnabledMarker::KEYWORD => enabled = input.parse()?,
_ => return Err( lookahead.error() ),
}
}
else
{
return Err( lookahead.error() );
}
// Optional comma handling
if input.peek( syn::Token![,] )
{
input.parse::< syn::Token![,] >()?;
}
}
Ok( MyAttributes { debug, enabled } )
}
}
let input : syn::Attribute = syn::parse_quote!( #[ attribute( enabled = true ) ] );
let meta = match input.meta
{
syn::Meta::List( meta_list ) => meta_list,
_ => panic!( "Expected a Meta::List" ),
};
let nested_meta_stream : proc_macro2::TokenStream = meta.tokens;
let attrs : MyAttributes = syn::parse2( nested_meta_stream ).unwrap();
println!( "{:?}", attrs );In this example, the AttributePropertyBoolean struct is used to define attributes with boolean properties.
The DebugMarker and EnabledMarker structs act as markers to distinguish between different boolean attributes.
The MyAttributes struct aggregates these boolean attributes.
The Parse implementation for MyAttributes iterates through the attribute’s key-value pairs,
identifying each by its marker’s keyword and parsing the boolean value.
It uses the ParseStream to parse identifiers and their associated values,
matching them to the appropriate marker’s keyword.
If an unrecognized identifier is encountered, it returns an error.
The parse_quote! macro is used to create a syn::Attribute instance with the attribute syntax,
which is then parsed into the MyAttributes struct. The resulting MyAttributes instance is printed to the console.
Implementations§
Source§impl<Marker> AttributePropertyBoolean<Marker>
impl<Marker> AttributePropertyBoolean<Marker>
Trait Implementations§
Source§impl<Marker> AsRef<bool> for AttributePropertyBoolean<Marker>
impl<Marker> AsRef<bool> for AttributePropertyBoolean<Marker>
Source§impl<Marker, IntoT> Assign<AttributePropertyBoolean<Marker>, IntoT> for AttributePropertyBoolean<Marker>where
IntoT: Into<AttributePropertyBoolean<Marker>>,
impl<Marker, IntoT> Assign<AttributePropertyBoolean<Marker>, IntoT> for AttributePropertyBoolean<Marker>where
IntoT: Into<AttributePropertyBoolean<Marker>>,
Source§impl<Marker> AttributePropertyComponent for AttributePropertyBoolean<Marker>where
Marker: AttributePropertyComponent,
impl<Marker> AttributePropertyComponent for AttributePropertyBoolean<Marker>where
Marker: AttributePropertyComponent,
Source§impl<Marker> Clone for AttributePropertyBoolean<Marker>where
Marker: Clone,
impl<Marker> Clone for AttributePropertyBoolean<Marker>where
Marker: Clone,
Source§fn clone(&self) -> AttributePropertyBoolean<Marker>
fn clone(&self) -> AttributePropertyBoolean<Marker>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<Marker> Debug for AttributePropertyBoolean<Marker>where
Marker: Debug,
impl<Marker> Debug for AttributePropertyBoolean<Marker>where
Marker: Debug,
Source§impl<Marker> Default for AttributePropertyBoolean<Marker>where
Marker: Default,
impl<Marker> Default for AttributePropertyBoolean<Marker>where
Marker: Default,
Source§fn default() -> AttributePropertyBoolean<Marker>
fn default() -> AttributePropertyBoolean<Marker>
Source§impl<Marker> Deref for AttributePropertyBoolean<Marker>
impl<Marker> Deref for AttributePropertyBoolean<Marker>
Source§impl<Marker> From<AttributePropertyBoolean<Marker>> for bool
impl<Marker> From<AttributePropertyBoolean<Marker>> for bool
Source§fn from(src: AttributePropertyBoolean<Marker>) -> bool
fn from(src: AttributePropertyBoolean<Marker>) -> bool
Source§impl<Marker> From<bool> for AttributePropertyBoolean<Marker>
impl<Marker> From<bool> for AttributePropertyBoolean<Marker>
Source§fn from(src: bool) -> AttributePropertyBoolean<Marker>
fn from(src: bool) -> AttributePropertyBoolean<Marker>
Source§impl<Marker> Parse for AttributePropertyBoolean<Marker>
impl<Marker> Parse for AttributePropertyBoolean<Marker>
fn parse( input: &ParseBuffer<'_>, ) -> Result<AttributePropertyBoolean<Marker>, Error>
impl<Marker> Copy for AttributePropertyBoolean<Marker>where
Marker: Copy,
Auto Trait Implementations§
impl<Marker> Freeze for AttributePropertyBoolean<Marker>
impl<Marker> RefUnwindSafe for AttributePropertyBoolean<Marker>where
Marker: RefUnwindSafe,
impl<Marker> Send for AttributePropertyBoolean<Marker>where
Marker: Send,
impl<Marker> Sync for AttributePropertyBoolean<Marker>where
Marker: Sync,
impl<Marker> Unpin for AttributePropertyBoolean<Marker>where
Marker: Unpin,
impl<Marker> UnwindSafe for AttributePropertyBoolean<Marker>where
Marker: UnwindSafe,
Blanket Implementations§
Source§impl<S> AssignWithType for S
impl<S> AssignWithType for S
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
impl<C, E> EntryToVal<C> for Ewhere
C: Collection<Entry = E>,
Source§type Val = <C as Collection>::Val
type Val = <C as Collection>::Val
Entry in complex collections.
For example, in a HashMap, while Entry might be a ( key, value ) tuple, Val might only be the value part.Source§fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
fn entry_to_val(self) -> <E as EntryToVal<C>>::Val
Source§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<Initial, Error, Final> TransitiveTryFrom<Error, Initial> for Final
impl<Initial, Error, Final> TransitiveTryFrom<Error, Initial> for Final
Source§impl<Error, Final, Initial> TransitiveTryInto<Error, Final> for Initial
impl<Error, Final, Initial> TransitiveTryInto<Error, Final> for Initial
Source§impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
impl<C, Val> ValToEntry<C> for Valwhere
C: CollectionValToEntry<Val>,
Source§fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
fn val_to_entry(self) -> <C as CollectionValToEntry<Val>>::Entry
Invokes the val_to_entry function of the CollectionValToEntry trait to convert the value to an entry.