Skip to main content

stringify_ident_proc_macro/
lib.rs

1#![no_std]
2
3extern crate alloc;
4use alloc::string::ToString;
5use proc_macro::{Literal, TokenStream, TokenTree};
6
7#[proc_macro]
8pub fn stringify_ident(stream: TokenStream) -> TokenStream {
9    let mut iter = stream.into_iter();
10
11    let Some(first) = iter.next() else {
12        panic!("no tt");
13    };
14
15    let TokenTree::Ident(ident) = first else {
16        panic!("first tt is not an ident");
17    };
18
19    if iter.next().is_some() {
20        panic!("only 1 tt allowed");
21    }
22
23    TokenTree::Literal(Literal::string(&ident.to_string())).into()
24}