xref: /openbmc/linux/rust/macros/concat_idents.rs (revision 67f3c209)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use proc_macro::{token_stream, Ident, TokenStream, TokenTree};
4 
5 use crate::helpers::expect_punct;
6 
7 fn expect_ident(it: &mut token_stream::IntoIter) -> Ident {
8     if let Some(TokenTree::Ident(ident)) = it.next() {
9         ident
10     } else {
11         panic!("Expected Ident")
12     }
13 }
14 
15 pub(crate) fn concat_idents(ts: TokenStream) -> TokenStream {
16     let mut it = ts.into_iter();
17     let a = expect_ident(&mut it);
18     assert_eq!(expect_punct(&mut it), ',');
19     let b = expect_ident(&mut it);
20     assert!(it.next().is_none(), "only two idents can be concatenated");
21     let res = Ident::new(&format!("{a}{b}"), b.span());
22     TokenStream::from_iter([TokenTree::Ident(res)])
23 }
24