#tuple #macro #generics #variadics

macro impl_variadics

a macro to generate repetitive idents etc. usually used to implement trait for tuples.

4 releases

0.3.0 May 6, 2024
0.2.0 May 6, 2024
0.1.2 Mar 24, 2024
0.1.1 Mar 23, 2024
0.1.0 Mar 23, 2024

#4 in #variadics

Download history 2/week @ 2024-07-17 5/week @ 2024-07-24 1/week @ 2024-08-21 6/week @ 2024-08-28 8/week @ 2024-09-18 12/week @ 2024-09-25 2/week @ 2024-10-02

320 downloads per month
Used in attr-parser-fn

Custom license

11KB
232 lines

Impl Variadic

A macro for generate variadic generics.

The syntax is similar to quote.

Example

impl_variadics! {
    ..4 "T*" => {
        impl<#(#T0),*> Display for TupleDisplay<(#(#T0,)*)>
        where
            #(#T0: Display,)*
        {
            fn fmt(&self, _f: &mut Formatter) -> Result {
                #(self.0.#index.fmt(_f)?;)*
                Ok(())
            }
        }
    };
    /*
    10..20 "Ty*pe" "my_index_*" "and_more_*" => {
        ...
    }
    */
}

it expands to

impl Display for TupleDisplay<()> {
    fn fmt(&self, _f: &mut Formatter) -> Result {
        Ok(())
    }
}
impl<T0> Display for TupleDisplay<(T0,)>
where
    T0: Display,
{
    fn fmt(&self, _f: &mut Formatter) -> Result {
        self.0 .0.fmt(_f)?;
        Ok(())
    }
}
impl<T0, T1> Display for TupleDisplay<(T0, T1)>
where
    T0: Display,
    T1: Display,
{
    fn fmt(&self, _f: &mut Formatter) -> Result {
        self.0 .0.fmt(_f)?;
        self.0 .1.fmt(_f)?;
        Ok(())
    }
}
impl<T0, T1, T2> Display for TupleDisplay<(T0, T1, T2)>
where
    T0: Display,
    T1: Display,
    T2: Display,
{
    fn fmt(&self, _f: &mut Formatter) -> Result {
        self.0 .0.fmt(_f)?;
        self.0 .1.fmt(_f)?;
        self.0 .2.fmt(_f)?;
        Ok(())
    }
}
  • ..4: maximum iterator count is 4, from 0. you can add lower bound like 2..10.
  • "T*": a custom identifier pattern. will replace all * with indexes. you can try other patterns like "Type" or "index_*".
  • #index: a builtin iterator gives 0 ~ max_index.
  • #length: a builtin integer equals to iterator length.
  • #T0: custom identifier. it gives T0, T1, T2 ... TN, where N is the
    upper bound of the range minus 2. it coresponding to pattern T*, replace all * with 0.

Dependencies

~250–700KB
~17K SLoC