Skip to content

Commit 1e0b1a0

Browse files
authored
Merge pull request #858 from nbdd0121/rust
rust: take str literal instead bstr literal in `module!` macro
2 parents 25fa03c + 593e659 commit 1e0b1a0

24 files changed

+123
-101
lines changed

drivers/android/rust_binder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ use {context::Context, thread::Thread};
2727

2828
module! {
2929
type: BinderModule,
30-
name: b"rust_binder",
31-
author: b"Wedson Almeida Filho",
32-
description: b"Android Binder",
33-
license: b"GPL",
30+
name: "rust_binder",
31+
author: "Wedson Almeida Filho",
32+
description: "Android Binder",
33+
license: "GPL",
3434
}
3535

3636
trait DeliverToRead {

drivers/char/hw_random/bcm2835_rng_rust.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ use kernel::{
99

1010
module_platform_driver! {
1111
type: RngDriver,
12-
name: b"bcm2835_rng_rust",
13-
author: b"Rust for Linux Contributors",
14-
description: b"BCM2835 Random Number Generator (RNG) driver",
15-
license: b"GPL v2",
12+
name: "bcm2835_rng_rust",
13+
author: "Rust for Linux Contributors",
14+
description: "BCM2835 Random Number Generator (RNG) driver",
15+
license: "GPL v2",
1616
}
1717

1818
struct RngDevice;

drivers/gpio/gpio_pl061_rust.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ impl power::Operations for PL061Device {
361361

362362
module_amba_driver! {
363363
type: PL061Device,
364-
name: b"pl061_gpio",
365-
author: b"Wedson Almeida Filho",
366-
license: b"GPL",
364+
name: "pl061_gpio",
365+
author: "Wedson Almeida Filho",
366+
license: "GPL",
367367
}

rust/kernel/amba.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ unsafe impl device::RawDevice for Device {
223223
///
224224
/// module_amba_driver! {
225225
/// type: MyDriver,
226-
/// name: b"module_name",
227-
/// author: b"Author name",
228-
/// license: b"GPL",
226+
/// name: "module_name",
227+
/// author: "Author name",
228+
/// license: "GPL",
229229
/// }
230230
/// ```
231231
#[macro_export]

rust/kernel/fs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -799,10 +799,10 @@ impl<T: Type + Sync> crate::Module for Module<T> {
799799
///
800800
/// module_fs! {
801801
/// type: MyFs,
802-
/// name: b"my_fs_kernel_module",
803-
/// author: b"Rust for Linux Contributors",
804-
/// description: b"My very own file system kernel module!",
805-
/// license: b"GPL",
802+
/// name: "my_fs_kernel_module",
803+
/// author: "Rust for Linux Contributors",
804+
/// description: "My very own file system kernel module!",
805+
/// license: "GPL",
806806
/// }
807807
///
808808
/// struct MyFs;

rust/kernel/miscdev.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ impl<T: file::Operations<OpenData = ()>> crate::Module for Module<T> {
266266
///
267267
/// module_misc_device! {
268268
/// type: MyFile,
269-
/// name: b"my_miscdev_kernel_module",
270-
/// author: b"Rust for Linux Contributors",
271-
/// description: b"My very own misc device kernel module!",
272-
/// license: b"GPL",
269+
/// name: "my_miscdev_kernel_module",
270+
/// author: "Rust for Linux Contributors",
271+
/// description: "My very own misc device kernel module!",
272+
/// license: "GPL",
273273
/// }
274274
///
275275
/// #[derive(Default)]

rust/kernel/platform.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,9 @@ unsafe impl device::RawDevice for Device {
210210
///
211211
/// module_platform_driver! {
212212
/// type: MyDriver,
213-
/// name: b"module_name",
214-
/// author: b"Author name",
215-
/// license: b"GPL",
213+
/// name: "module_name",
214+
/// author: "Author name",
215+
/// license: "GPL",
216216
/// }
217217
/// ```
218218
#[macro_export]

rust/macros/helpers.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ pub(crate) fn try_byte_string(it: &mut token_stream::IntoIter) -> Option<String>
2828
})
2929
}
3030

31+
pub(crate) fn try_string(it: &mut token_stream::IntoIter) -> Option<String> {
32+
try_literal(it).and_then(|string| {
33+
if string.starts_with('\"') && string.ends_with('\"') {
34+
let content = &string[1..string.len() - 1];
35+
if content.contains('\\') {
36+
panic!("Escape sequences in string literals not yet handled");
37+
}
38+
Some(content.to_string())
39+
} else if string.starts_with("r\"") {
40+
panic!("Raw string literals are not yet handled");
41+
} else {
42+
None
43+
}
44+
})
45+
}
46+
3147
pub(crate) fn expect_ident(it: &mut token_stream::IntoIter) -> String {
3248
try_ident(it).expect("Expected Ident")
3349
}
@@ -52,8 +68,14 @@ pub(crate) fn expect_group(it: &mut token_stream::IntoIter) -> Group {
5268
}
5369
}
5470

55-
pub(crate) fn expect_byte_string(it: &mut token_stream::IntoIter) -> String {
56-
try_byte_string(it).expect("Expected byte string")
71+
pub(crate) fn expect_string(it: &mut token_stream::IntoIter) -> String {
72+
try_string(it).expect("Expected string")
73+
}
74+
75+
pub(crate) fn expect_string_ascii(it: &mut token_stream::IntoIter) -> String {
76+
let string = try_string(it).expect("Expected string");
77+
assert!(string.is_ascii(), "Expected ASCII string");
78+
string
5779
}
5880

5981
pub(crate) fn expect_end(it: &mut token_stream::IntoIter) {
@@ -70,10 +92,10 @@ pub(crate) fn get_literal(it: &mut token_stream::IntoIter, expected_name: &str)
7092
literal
7193
}
7294

73-
pub(crate) fn get_byte_string(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
95+
pub(crate) fn get_string(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
7496
assert_eq!(expect_ident(it), expected_name);
7597
assert_eq!(expect_punct(it), ':');
76-
let byte_string = expect_byte_string(it);
98+
let string = expect_string(it);
7799
assert_eq!(expect_punct(it), ',');
78-
byte_string
100+
string
79101
}

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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,13 @@ impl ModuleInfo {
256256

257257
match key.as_str() {
258258
"type" => info.type_ = expect_ident(it),
259-
"name" => info.name = expect_byte_string(it),
260-
"author" => info.author = Some(expect_byte_string(it)),
261-
"description" => info.description = Some(expect_byte_string(it)),
262-
"license" => info.license = expect_byte_string(it),
263-
"alias" => info.alias = Some(expect_byte_string(it)),
259+
"name" => info.name = expect_string_ascii(it),
260+
"author" => info.author = Some(expect_string(it)),
261+
"description" => info.description = Some(expect_string(it)),
262+
"license" => info.license = expect_string_ascii(it),
263+
"alias" => info.alias = Some(expect_string_ascii(it)),
264264
"alias_rtnl_link" => {
265-
info.alias = Some(format!("rtnl-link-{}", expect_byte_string(it)))
265+
info.alias = Some(format!("rtnl-link-{}", expect_string_ascii(it)))
266266
}
267267
"params" => info.params = Some(expect_group(it)),
268268
_ => panic!(
@@ -347,7 +347,7 @@ pub(crate) fn module(ts: TokenStream) -> TokenStream {
347347
let mut param_it = group.stream().into_iter();
348348
let param_default = get_default(&param_type, &mut param_it);
349349
let param_permissions = get_literal(&mut param_it, "permissions");
350-
let param_description = get_byte_string(&mut param_it, "description");
350+
let param_description = get_string(&mut param_it, "description");
351351
expect_end(&mut param_it);
352352

353353
// TODO: More primitive types.

0 commit comments

Comments
 (0)