Skip to content

Commit 39c58b0

Browse files
nbdd0121BennoLossin
authored andcommitted
rust: macros: convert concat_idents! to use syn
This eliminates the need for `expect_punct` helper. Reviewed-by: Tamir Duberstein <tamird@gmail.com> Reviewed-by: Benno Lossin <lossin@kernel.org> Signed-off-by: Gary Guo <gary@garyguo.net>
1 parent 9733f4c commit 39c58b0

3 files changed

Lines changed: 29 additions & 28 deletions

File tree

rust/macros/concat_idents.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
use proc_macro2::{token_stream, Ident, TokenStream, TokenTree};
3+
use proc_macro2::{
4+
Ident,
5+
TokenStream,
6+
TokenTree, //
7+
};
8+
use syn::{
9+
parse::{
10+
Parse,
11+
ParseStream, //
12+
},
13+
Result,
14+
Token, //
15+
};
416

5-
use crate::helpers::expect_punct;
17+
pub(crate) struct Input {
18+
a: Ident,
19+
_comma: Token![,],
20+
b: Ident,
21+
}
622

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")
23+
impl Parse for Input {
24+
fn parse(input: ParseStream<'_>) -> Result<Self> {
25+
Ok(Self {
26+
a: input.parse()?,
27+
_comma: input.parse()?,
28+
b: input.parse()?,
29+
})
1230
}
1331
}
1432

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");
33+
pub(crate) fn concat_idents(Input { a, b, .. }: Input) -> TokenStream {
2134
let res = Ident::new(&format!("{a}{b}"), b.span());
2235
TokenStream::from_iter([TokenTree::Ident(res)])
2336
}

rust/macros/helpers.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
use proc_macro2::{
4-
token_stream,
5-
TokenStream,
6-
TokenTree, //
7-
};
3+
use proc_macro2::TokenStream;
84
use quote::ToTokens;
95
use syn::{
106
parse::{
@@ -16,14 +12,6 @@ use syn::{
1612
Result, //
1713
};
1814

19-
pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char {
20-
if let TokenTree::Punct(punct) = it.next().expect("Reached end of token stream for Punct") {
21-
punct.as_char()
22-
} else {
23-
panic!("Expected Punct");
24-
}
25-
}
26-
2715
/// A string literal that is required to have ASCII value only.
2816
pub(crate) struct AsciiLitStr(LitStr);
2917

rust/macros/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ pub fn fmt(input: TokenStream) -> TokenStream {
311311
/// assert_eq!(BR_OK, binder_driver_return_protocol_BR_OK);
312312
/// ```
313313
#[proc_macro]
314-
pub fn concat_idents(ts: TokenStream) -> TokenStream {
315-
concat_idents::concat_idents(ts.into()).into()
314+
pub fn concat_idents(input: TokenStream) -> TokenStream {
315+
concat_idents::concat_idents(parse_macro_input!(input)).into()
316316
}
317317

318318
/// Paste identifiers together.

0 commit comments

Comments
 (0)