Skip to content

Commit 593e659

Browse files
committed
rust: take str literal instead bstr literal in module! macro
For simplicity (avoid parsing), escape sequences and raw string literals are not yet handled. Module names, aliases and license strings are restricted to ASCII only. Link: #252 Link: https://lore.kernel.org/lkml/[email protected]/ Signed-off-by: Gary Guo <[email protected]>
1 parent 25fa03c commit 593e659

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.

samples/rust/rust_chrdev.rs

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

88
module! {
99
type: RustChrdev,
10-
name: b"rust_chrdev",
11-
author: b"Rust for Linux Contributors",
12-
description: b"Rust character device sample",
13-
license: b"GPL",
10+
name: "rust_chrdev",
11+
author: "Rust for Linux Contributors",
12+
description: "Rust character device sample",
13+
license: "GPL",
1414
}
1515

1616
struct RustFile;

samples/rust/rust_echo_server.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ impl kernel::Module for RustEchoServer {
5353

5454
module! {
5555
type: RustEchoServer,
56-
name: b"rust_echo_server",
57-
author: b"Rust for Linux Contributors",
58-
description: b"Rust tcp echo sample",
59-
license: b"GPL v2",
56+
name: "rust_echo_server",
57+
author: "Rust for Linux Contributors",
58+
description: "Rust tcp echo sample",
59+
license: "GPL v2",
6060
}

samples/rust/rust_fs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ use kernel::{c_str, fs};
77

88
module_fs! {
99
type: RustFs,
10-
name: b"rust_fs",
11-
author: b"Rust for Linux Contributors",
12-
license: b"GPL",
10+
name: "rust_fs",
11+
author: "Rust for Linux Contributors",
12+
license: "GPL",
1313
}
1414

1515
struct RustFs;

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_miscdev.rs

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

1313
module! {
1414
type: RustMiscdev,
15-
name: b"rust_miscdev",
16-
author: b"Rust for Linux Contributors",
17-
description: b"Rust miscellaneous device sample",
18-
license: b"GPL",
15+
name: "rust_miscdev",
16+
author: "Rust for Linux Contributors",
17+
description: "Rust miscellaneous device sample",
18+
license: "GPL",
1919
}
2020

2121
const MAX_TOKENS: usize = 3;

samples/rust/rust_module_parameters.rs

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

77
module! {
88
type: RustModuleParameters,
9-
name: b"rust_module_parameters",
10-
author: b"Rust for Linux Contributors",
11-
description: b"Rust module parameters sample",
12-
license: b"GPL",
9+
name: "rust_module_parameters",
10+
author: "Rust for Linux Contributors",
11+
description: "Rust module parameters sample",
12+
license: "GPL",
1313
params: {
1414
my_bool: bool {
1515
default: true,
1616
permissions: 0,
17-
description: b"Example of bool",
17+
description: "Example of bool",
1818
},
1919
my_i32: i32 {
2020
default: 42,
2121
permissions: 0o644,
22-
description: b"Example of i32",
22+
description: "Example of i32",
2323
},
2424
my_str: str {
2525
default: b"default str val",
2626
permissions: 0o644,
27-
description: b"Example of a string param",
27+
description: "Example of a string param",
2828
},
2929
my_usize: usize {
3030
default: 42,
3131
permissions: 0o644,
32-
description: b"Example of usize",
32+
description: "Example of usize",
3333
},
3434
my_array: ArrayParam<i32, 3> {
3535
default: [0, 1],
3636
permissions: 0,
37-
description: b"Example of array",
37+
description: "Example of array",
3838
},
3939
},
4040
}

samples/rust/rust_netfilter.rs

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

99
module! {
1010
type: RustNetfilter,
11-
name: b"rust_netfilter",
12-
author: b"Rust for Linux Contributors",
13-
description: b"Rust netfilter sample",
14-
license: b"GPL",
11+
name: "rust_netfilter",
12+
author: "Rust for Linux Contributors",
13+
description: "Rust netfilter sample",
14+
license: "GPL",
1515
}
1616

1717
struct RustNetfilter {

samples/rust/rust_platform.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use kernel::{module_platform_driver, of, platform, prelude::*};
66

77
module_platform_driver! {
88
type: Driver,
9-
name: b"rust_platform",
10-
license: b"GPL",
9+
name: "rust_platform",
10+
license: "GPL",
1111
}
1212

1313
struct Driver;

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::{pr_cont, str::CStr, ThisModule};
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;

samples/rust/rust_random.rs

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

1414
module_misc_device! {
1515
type: RandomFile,
16-
name: b"rust_random",
17-
author: b"Rust for Linux Contributors",
18-
description: b"Just use /dev/urandom: Now with early-boot safety",
19-
license: b"GPL",
16+
name: "rust_random",
17+
author: "Rust for Linux Contributors",
18+
description: "Just use /dev/urandom: Now with early-boot safety",
19+
license: "GPL",
2020
}
2121

2222
struct RandomFile;

samples/rust/rust_selftests.rs

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

99
module! {
1010
type: RustSelftests,
11-
name: b"rust_selftests",
12-
author: b"Rust for Linux Contributors",
13-
description: b"Self test cases for Rust",
14-
license: b"GPL",
11+
name: "rust_selftests",
12+
author: "Rust for Linux Contributors",
13+
description: "Self test cases for Rust",
14+
license: "GPL",
1515
}
1616

1717
struct RustSelftests;

0 commit comments

Comments
 (0)