diff options
| author | Trevor Gross <[email protected]> | 2023-11-18 01:39:59 +0000 |
|---|---|---|
| committer | Miguel Ojeda <[email protected]> | 2023-12-14 19:14:01 +0000 |
| commit | 2dc318ea9681c14c37bad2715097df4380a3c547 (patch) | |
| tree | 08e7100ead1979309465902f09c227c0e736f5f2 /rust/macros/paste.rs | |
| parent | rust: bindings: rename const binding using sed (diff) | |
| download | kernel-2dc318ea9681c14c37bad2715097df4380a3c547.tar.gz kernel-2dc318ea9681c14c37bad2715097df4380a3c547.zip | |
rust: macros: update 'paste!' macro to accept string literals
Enable combining identifiers with literals in the 'paste!' macro. This
allows combining user-specified strings with affixes to create
namespaced identifiers.
This sample code:
macro_rules! m {
($name:lit) => {
paste!(struct [<_some_ $name _struct_>] {})
}
}
m!("foo_bar");
Would previously cause a compilation error. It will now generate:
struct _some_foo_bar_struct_ {}
Signed-off-by: Trevor Gross <[email protected]>
Reviewed-by: Martin Rodriguez Reboredo <[email protected]>
Reviewed-by: Vincenzo Palazzo <[email protected]>
Reviewed-by: Alice Ryhl <[email protected]>
Reviewed-by: Benno Lossin <[email protected]>
Reviewed-by: Gary Guo <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
[ Added `:` before example block. ]
Signed-off-by: Miguel Ojeda <[email protected]>
Diffstat (limited to 'rust/macros/paste.rs')
| -rw-r--r-- | rust/macros/paste.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/rust/macros/paste.rs b/rust/macros/paste.rs index 385a78434224..f40d42b35b58 100644 --- a/rust/macros/paste.rs +++ b/rust/macros/paste.rs @@ -9,7 +9,15 @@ fn concat(tokens: &[TokenTree], group_span: Span) -> TokenTree { loop { match tokens.next() { None => break, - Some(TokenTree::Literal(lit)) => segments.push((lit.to_string(), lit.span())), + Some(TokenTree::Literal(lit)) => { + // Allow us to concat string literals by stripping quotes + let mut value = lit.to_string(); + if value.starts_with('"') && value.ends_with('"') { + value.remove(0); + value.pop(); + } + segments.push((value, lit.span())); + } Some(TokenTree::Ident(ident)) => { let mut value = ident.to_string(); if value.starts_with("r#") { |
