Macro tt_call::tt_replace
source · macro_rules! tt_replace { { $caller:tt condition = [{ $($condition:ident)::* }] replace_with = [{ $($with:tt)* }] input = [{ $($input:tt)* }] } => { ... }; }
Expand description
Replace each token that matches a given predicate by a given replacement sequence of tokens. [tt-call]
§Input
condition = [{
name of predicate macro}]
replace_with = [{
arbitrary tokens inserted when the predicate is true}]
input = [{
arbitrary input tokens}]
The predicate macro must accept a single input value named input
. It is
expected to return a single output value which may have any name but must
hold the tokens true
or false
. For example the built-in tt_is_ident!
predicate expands to is_ident = [{ true }]
or is_ident = [{ false }]
.
§Output
tokens = [{
tokens after replacement}]
§Example
use tt_call::{tt_call, tt_replace, tt_return};
macro_rules! is_lowercase_self {
{
$caller:tt
input = [{ self }]
} => {
tt_return! {
$caller
is = [{ true }]
}
};
{
$caller:tt
input = [{ $other:tt }]
} => {
tt_return! {
$caller
is = [{ false }]
}
};
}
macro_rules! closure {
($($expr:tt)+) => {
|__value| tt_call! {
macro = [{ tt_replace }]
condition = [{ is_lowercase_self }]
replace_with = [{ __value }]
input = [{ $($expr)+ }]
}
};
}
fn main() {
let add_one = closure!(self + 1);
println!("{}", add_one(1));
}