count_tys!() function-like macro

Returns the count of comma-delimited [:ty]s (types) in the given [TokenStream] as a constant expression of type [usize]

Arguments

Examples

Basic usage

rust // count_tys!($($ty:ty),*)

More complete example

Cargo.toml

rust /* [dependencies] proc-macro-hack = "0.5" count-tys = "0.1" */

main.rs

```rust extern crate procmacrohack; use procmacrohack::procmacrohack;

[procmacrohack]

use counttts::counttys;

// It not necessarily must be a struct, it could be a generic // Read more about macrorules! here: // https://doc.rust-lang.org/rust-by-example/macros.html macrorules! declarevariadicstruct { ($structname:ident, <$($ty:ty),*>) => { struct $structname { // fields }

    impl $struct_name {
        pub const fn count() -> usize {
            // count_tys!() can be used in an expression and even
            // const expression context
            // unlike macros without proc_macro_hack
            // note: see issue #54727
            // <https://github.com/rust-lang/rust/issues/54727>
            // for more information.
            count_tys!($($ty:ty),*)
        }
    }
};

}

// declarevariadicstruct!(VariadicStruct, ); // expands into the following: // // struct VariadicStruct { // // fields // } // // impl VariadicStuct { // pub const fn count() -> usize { // 3usize // } // } declarevariadicstruct!(VariadicStruct, ); assert_eq!(VariadicStruct::count(), 3); ```