Skip to content

Commit

Permalink
Merge pull request #198 from dtolnay/underscore
Browse files Browse the repository at this point in the history
Recognize underscore token to avoid slow path
  • Loading branch information
dtolnay authored Dec 27, 2021
2 parents 6e6bf56 + 53109e8 commit f45df31
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,10 @@ macro_rules! quote_token {
$crate::__private::push_literal(&mut $tokens, stringify!($literal));
};

($tokens:ident _) => {
$crate::__private::push_underscore(&mut $tokens);
};

($tokens:ident $other:tt) => {
$crate::__private::parse(&mut $tokens, stringify!($other));
};
Expand Down Expand Up @@ -1297,6 +1301,10 @@ macro_rules! quote_token_spanned {
$crate::__private::push_literal_spanned(&mut $tokens, $span, stringify!($literal));
};

($tokens:ident $span:ident _) => {
$crate::__private::push_underscore_spanned(&mut $tokens, $span);
};

($tokens:ident $span:ident $other:tt) => {
$crate::__private::parse_spanned(&mut $tokens, $span, stringify!($other));
};
Expand Down
8 changes: 8 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ push_punct!(push_star push_star_spanned '*');
push_punct!(push_sub push_sub_spanned '-');
push_punct!(push_sub_eq push_sub_eq_spanned '-' '=');

pub fn push_underscore(tokens: &mut TokenStream) {
push_underscore_spanned(tokens, Span::call_site());
}

pub fn push_underscore_spanned(tokens: &mut TokenStream, span: Span) {
tokens.append(Ident::new("_", span));
}

// Helper method for constructing identifiers from the `format_ident!` macro,
// handling `r#` prefixes.
//
Expand Down
7 changes: 7 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,13 @@ fn test_ident() {
assert_eq!(expected, tokens.to_string());
}

#[test]
fn test_underscore() {
let tokens = quote!(let _;);
let expected = "let _ ;";
assert_eq!(expected, tokens.to_string());
}

#[test]
fn test_duplicate() {
let ch = 'x';
Expand Down

0 comments on commit f45df31

Please sign in to comment.