[][src]Macro tt_equal::tt_equal

tt_equal!() { /* proc-macro */ }

A predicate for whether two token trees are equal. [tt-call]

Given two token trees, it compares them and returns whether they are equal. Intended for use with tt_if.

Input

  • input = [{ exactly two token trees }]

Output

  • is_equal = [{ either true or false }]

Example

use tt_equal::tt_equal;
use tt_call::tt_if;

macro_rules! same_ident{
    {
        $id1:ident, $id2:ident
    } => {
        tt_if!{
            condition = [{tt_equal}]
            input = [{ $id1 $id2 }]         // The two identifiers are here passed to 'tt_equal'
            true = [{
                const $id1: bool = true;
            }]
            false = [{
                const $id1: bool = false;
            }]
        }
    }
}

same_ident!(AN_IDENT, AN_IDENT);            // Equal identifiers result in a true constant
same_ident!(A_DIFFERENT_IDENT, AN_IDENT);   // Different identifiers result in a false constant

fn main() {
    assert_eq!(AN_IDENT, true);
    assert_eq!(A_DIFFERENT_IDENT, false);
}

Caveat

This is a procedural macro and therefore has corresponding restrictions on where it can be used. E.g. As of Rust 1.37, it cannot be used within an expression context.

Hint

This macro only accepts a single token tree on each 'side' of the comparison. To compare multiple token trees, parantheses, brackets, or braces can be used to wrap the tokens and make them into a single token tree.

Example:

use tt_equal::tt_equal;
use tt_call::tt_if;

tt_if!{
    condition = [{tt_equal}]
    input = [{ (Two tokens) (Two tokens) }]
    true = [{
	    const SHOULD_BE_TRUE: bool = true;
    }]
    false = [{
	    const SHOULD_BE_TRUE: bool = false;
    }]
}

tt_if!{
    condition = [{tt_equal}]
    input = [{ (Two tokens) (Three tokens here) }]
    true = [{
	    const SHOULD_BE_FALSE: bool = true;
    }]
    false = [{
	    const SHOULD_BE_FALSE: bool = false;
    }]
}

fn main() {
    assert_eq!(SHOULD_BE_TRUE, true);
    assert_eq!(SHOULD_BE_FALSE, false);
}