Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recognize underscore token to avoid slow path #198

Merged
merged 2 commits into from Dec 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/lib.rs
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
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
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