Skip to content

Instantly share code, notes, and snippets.

@AnthonyMikh
Created August 8, 2020 13:47
Show Gist options
  • Save AnthonyMikh/6df57eb0ffbd9d52d0b24d156ed00e08 to your computer and use it in GitHub Desktop.
Save AnthonyMikh/6df57eb0ffbd9d52d0b24d156ed00e08 to your computer and use it in GitHub Desktop.
#![feature(trace_macros)]
trace_macros!(true);
macro_rules! assert_ct_not_equal {
($a:tt, $b:tt) => {
#[deny(const_err)]
const _: () = {
macro_rules! is_a {
($a) => { true };
($b) => { false };
};
[()][is_a!($b) as usize]
};
}
}
macro_rules! assert_ct_all_different {
() => {};
($single:tt) => {};
($first:tt, $($rest:tt,)*) => {
#[deny(const_err)]
const _: () = {
$(
assert_ct_not_equal!($first, $rest);
)*
};
assert_ct_all_different!($($rest,)*);
}
}
macro_rules! assert_all_literals {
($($l:literal,)*) => {};
}
macro_rules! assert_ct_all_different_literals {
($($tok:tt),*) => {
assert_ct_all_different_literals!($($tok,)*);
};
($($tok:tt,)*) => {
assert_all_literals!($($tok,)*);
assert_ct_all_different!($($tok,)*);
};
}
macro_rules! make_translation_macro {
($name:ident : $($original:literal => $translation:literal),*) => {
make_translation_macro!($name : $($original => $translation,)*);
};
($name:ident : $($original:literal => $translation:literal,)*) => {
assert_ct_all_different_literals!($($original,)*);
macro_rules! $name {
$(($original) => { $translation };)*
}
}
}
make_translation_macro!(tr:
"foo" => "фу",
"bar" => "бар"
);
const _: &str = tr!("foo");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment