Skip to content

Commit b13c988

Browse files
nbdd0121ojeda
authored andcommitted
rust: macros: take string literals in module!
Instead of taking binary string literals, take string ones instead, making it easier for users to define a module, i.e. instead of calling `module!` like: module! { ... name: b"rust_minimal", ... } now it is called as: module! { ... name: "rust_minimal", ... } Module names, aliases and license strings are restricted to ASCII only. However, the author and the description allows UTF-8. For simplicity (avoid parsing), escape sequences and raw string literals are not yet handled. Link: Rust-for-Linux#252 Link: https://lore.kernel.org/lkml/[email protected]/ Signed-off-by: Gary Guo <[email protected]> [Reworded, adapted for upstream and applied latest changes] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent b44becc commit b13c988

File tree

5 files changed

+37
-25
lines changed

5 files changed

+37
-25
lines changed

rust/macros/helpers.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,16 @@ pub(crate) fn try_literal(it: &mut token_stream::IntoIter) -> Option<String> {
1818
}
1919
}
2020

21-
pub(crate) fn try_byte_string(it: &mut token_stream::IntoIter) -> Option<String> {
22-
try_literal(it).and_then(|byte_string| {
23-
if byte_string.starts_with("b\"") && byte_string.ends_with('\"') {
24-
Some(byte_string[2..byte_string.len() - 1].to_string())
21+
pub(crate) fn try_string(it: &mut token_stream::IntoIter) -> Option<String> {
22+
try_literal(it).and_then(|string| {
23+
if string.starts_with('\"') && string.ends_with('\"') {
24+
let content = &string[1..string.len() - 1];
25+
if content.contains('\\') {
26+
panic!("Escape sequences in string literals not yet handled");
27+
}
28+
Some(content.to_string())
29+
} else if string.starts_with("r\"") {
30+
panic!("Raw string literals are not yet handled");
2531
} else {
2632
None
2733
}
@@ -40,8 +46,14 @@ pub(crate) fn expect_punct(it: &mut token_stream::IntoIter) -> char {
4046
}
4147
}
4248

43-
pub(crate) fn expect_byte_string(it: &mut token_stream::IntoIter) -> String {
44-
try_byte_string(it).expect("Expected byte string")
49+
pub(crate) fn expect_string(it: &mut token_stream::IntoIter) -> String {
50+
try_string(it).expect("Expected string")
51+
}
52+
53+
pub(crate) fn expect_string_ascii(it: &mut token_stream::IntoIter) -> String {
54+
let string = try_string(it).expect("Expected string");
55+
assert!(string.is_ascii(), "Expected ASCII string");
56+
string
4557
}
4658

4759
pub(crate) fn expect_end(it: &mut token_stream::IntoIter) {

rust/macros/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ use proc_macro::TokenStream;
2525
///
2626
/// module!{
2727
/// type: MyModule,
28-
/// name: b"my_kernel_module",
29-
/// author: b"Rust for Linux Contributors",
30-
/// description: b"My very own kernel module!",
31-
/// license: b"GPL",
28+
/// name: "my_kernel_module",
29+
/// author: "Rust for Linux Contributors",
30+
/// description: "My very own kernel module!",
31+
/// license: "GPL",
3232
/// params: {
3333
/// my_i32: i32 {
3434
/// default: 42,
3535
/// permissions: 0o000,
36-
/// description: b"Example of i32",
36+
/// description: "Example of i32",
3737
/// },
3838
/// writeable_i32: i32 {
3939
/// default: 42,
4040
/// permissions: 0o644,
41-
/// description: b"Example of i32",
41+
/// description: "Example of i32",
4242
/// },
4343
/// },
4444
/// }

rust/macros/module.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,11 @@ impl ModuleInfo {
108108

109109
match key.as_str() {
110110
"type" => info.type_ = expect_ident(it),
111-
"name" => info.name = expect_byte_string(it),
112-
"author" => info.author = Some(expect_byte_string(it)),
113-
"description" => info.description = Some(expect_byte_string(it)),
114-
"license" => info.license = expect_byte_string(it),
115-
"alias" => info.alias = Some(expect_byte_string(it)),
111+
"name" => info.name = expect_string_ascii(it),
112+
"author" => info.author = Some(expect_string(it)),
113+
"description" => info.description = Some(expect_string(it)),
114+
"license" => info.license = expect_string_ascii(it),
115+
"alias" => info.alias = Some(expect_string_ascii(it)),
116116
_ => panic!(
117117
"Unknown key \"{}\". Valid keys are: {:?}.",
118118
key, EXPECTED_KEYS

samples/rust/rust_minimal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use kernel::prelude::*;
66

77
module! {
88
type: RustMinimal,
9-
name: b"rust_minimal",
10-
author: b"Rust for Linux Contributors",
11-
description: b"Rust minimal sample",
12-
license: b"GPL",
9+
name: "rust_minimal",
10+
author: "Rust for Linux Contributors",
11+
description: "Rust minimal sample",
12+
license: "GPL",
1313
}
1414

1515
struct RustMinimal {

samples/rust/rust_print.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use kernel::prelude::*;
77

88
module! {
99
type: RustPrint,
10-
name: b"rust_print",
11-
author: b"Rust for Linux Contributors",
12-
description: b"Rust printing macros sample",
13-
license: b"GPL",
10+
name: "rust_print",
11+
author: "Rust for Linux Contributors",
12+
description: "Rust printing macros sample",
13+
license: "GPL",
1414
}
1515

1616
struct RustPrint;

0 commit comments

Comments
 (0)