Macro tstr::alias

source ·
macro_rules! alias {
    (
        $(
            $(#[$attr:meta])*
            $vis:vis $name:ident = $expr:tt
        );*
        $(;)?
    ) => { ... };
}
Expand description

Declares const and type aliases for type-level strings.

§String Arguments

You can alias either one type-level string, or a tuple of type-level strings

tstr::alias!{
    // Aliases the "bar" type-level string as both a const and a type, named Bar.
    pub Bar = bar;

    // Aliases the "0" type-level string.
    pub N0 = 0;

    // Aliases the ("foo", "baz") tuple of type-level strings,
    // Equivalent to `TS!(foo, baz)` and `ts!("foo", "baz")`
    pub Tup = (foo, baz);
}

§Examples

§Indexing

This uses types from the for_examples module, which can be seen in the docs with the “for_examples” feature.

use std::ops::Index;

use tstr::for_examples::{Foo, Bar};

tstr::alias!{
    // Declares both an NBar type alias and an NBar constant of that type.
    pub NBar = bar;

    // Declares both an NBaz type alias and an NBaz constant of that type.
    pub NBaz = "baz";

    // Declares both an NQux type alias and an NQux constant of that type.
    pub NQux = "qux";

}

// The macro can also be invoked like this
tstr::alias!{ pub NBoom = boom }

let this = Foo::new(3, 5, "8");
assert_eq!(get_bar_baz(&this), (3, 5));

let other = Bar::new(13, false, Some('C'));
assert_eq!(get_bar_baz(&other), (13, false));


type IndexOut<T, N> = <T as Index<N>>::Output;

fn get_bar_baz<T>(this: &T) -> (IndexOut<T, NBar>, IndexOut<T, NBaz>)
where
    T: Index<NBar> + Index<NBaz>,
    IndexOut<T, NBar>: Copy,
    IndexOut<T, NBaz>: Copy,
{
    (this[NBar], this[NBaz])
}