Struct AttributePropertyBoolean

Source
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>

Source

pub fn internal(self) -> bool

Just unwraps and returns the internal data.

Source

pub fn ref_internal(&self) -> &bool

Returns a reference to the internal boolean value.

Trait Implementations§

Source§

impl<Marker> AsRef<bool> for AttributePropertyBoolean<Marker>

Source§

fn as_ref(&self) -> &bool

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<Marker, IntoT> Assign<AttributePropertyBoolean<Marker>, IntoT> for AttributePropertyBoolean<Marker>
where IntoT: Into<AttributePropertyBoolean<Marker>>,

Source§

fn assign(&mut self, component: IntoT)

Sets or replaces the component on the object with the given value. Read more
Source§

fn impute(self, component: IntoT) -> Self
where Self: Sized,

Sets or replaces the component on the object with the given value. Unlike function (assing) function (impute) also consumes self and return it what is useful for builder pattern.
Source§

impl<Marker> AttributePropertyComponent for AttributePropertyBoolean<Marker>

Source§

const KEYWORD: &'static str = Marker::KEYWORD

The keyword that identifies the component. Read more
Source§

impl<Marker: Clone> Clone for AttributePropertyBoolean<Marker>

Source§

fn clone(&self) -> AttributePropertyBoolean<Marker>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<Marker: Debug> Debug for AttributePropertyBoolean<Marker>

Source§

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

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

impl<Marker: Default> Default for AttributePropertyBoolean<Marker>

Source§

fn default() -> AttributePropertyBoolean<Marker>

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

impl<Marker> Deref for AttributePropertyBoolean<Marker>

Source§

type Target = bool

The resulting type after dereferencing.
Source§

fn deref(&self) -> &bool

Dereferences the value.
Source§

impl<Marker> From<AttributePropertyBoolean<Marker>> for bool

Source§

fn from(src: AttributePropertyBoolean<Marker>) -> Self

Converts to this type from the input type.
Source§

impl<Marker> From<bool> for AttributePropertyBoolean<Marker>

Source§

fn from(src: bool) -> Self

Converts to this type from the input type.
Source§

impl<Marker> Parse for AttributePropertyBoolean<Marker>

Source§

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

Source§

impl<Marker: Copy> Copy for AttributePropertyBoolean<Marker>

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<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<S> AssignWithType for S

Source§

fn assign_with_type<T, IntoT>(&mut self, component: IntoT)
where IntoT: Into<T>, S: Assign<T, IntoT>,

Sets the value of a component by its type. 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> CloneDyn for T
where T: Clone,

Source§

fn __clone_dyn(&self, _: DontCallMe) -> *mut ()

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

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

Source§

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>,

Source§

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.