Skip to content

Commit ab4c335

Browse files
committed
rust: accept str instead of bstr in module!
Signed-off-by: Gary Guo <[email protected]>
1 parent fd4ef65 commit ab4c335

13 files changed

+103
-100
lines changed

drivers/android/rust_binder.rs

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

3232
module! {
3333
type: BinderModule,
34-
name: b"rust_binder",
35-
author: b"Wedson Almeida Filho",
36-
description: b"Android Binder",
37-
license: b"GPL v2",
34+
name: "rust_binder",
35+
author: "Wedson Almeida Filho",
36+
description: "Android Binder",
37+
license: "GPL v2",
3838
}
3939

4040
enum Either<L, R> {

drivers/char/hw_random/bcm2835_rng_rust.rs

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

1414
module! {
1515
type: RngModule,
16-
name: b"bcm2835_rng_rust",
17-
author: b"Rust for Linux Contributors",
18-
description: b"BCM2835 Random Number Generator (RNG) driver",
19-
license: b"GPL v2",
16+
name: "bcm2835_rng_rust",
17+
author: "Rust for Linux Contributors",
18+
description: "BCM2835 Random Number Generator (RNG) driver",
19+
license: "GPL v2",
2020
}
2121

2222
struct RngModule {

rust/macros/lib.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ use proc_macro::TokenStream;
2222
///
2323
/// module!{
2424
/// type: MyKernelModule,
25-
/// name: b"my_kernel_module",
26-
/// author: b"Rust for Linux Contributors",
27-
/// description: b"My very own kernel module!",
28-
/// license: b"GPL v2",
25+
/// name: "my_kernel_module",
26+
/// author: "Rust for Linux Contributors",
27+
/// description: "My very own kernel module!",
28+
/// license: "GPL v2",
2929
/// params: {
3030
/// my_i32: i32 {
3131
/// default: 42,
3232
/// permissions: 0o000,
33-
/// description: b"Example of i32",
33+
/// description: "Example of i32",
3434
/// },
3535
/// writeable_i32: i32 {
3636
/// default: 42,
3737
/// permissions: 0o644,
38-
/// description: b"Example of i32",
38+
/// description: "Example of i32",
3939
/// },
4040
/// },
4141
/// }
@@ -108,10 +108,10 @@ pub fn module(ts: TokenStream) -> TokenStream {
108108
///
109109
/// module_misc_device! {
110110
/// type: MyFile,
111-
/// name: b"my_miscdev_kernel_module",
112-
/// author: b"Rust for Linux Contributors",
113-
/// description: b"My very own misc device kernel module!",
114-
/// license: b"GPL v2",
111+
/// name: "my_miscdev_kernel_module",
112+
/// author: "Rust for Linux Contributors",
113+
/// description: "My very own misc device kernel module!",
114+
/// license: "GPL v2",
115115
/// }
116116
///
117117
/// #[derive(Default)]

rust/macros/module.rs

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
#![deny(clippy::perf)]
66
#![deny(clippy::style)]
77

8-
use proc_macro::{token_stream, Delimiter, Group, TokenStream, TokenTree};
8+
use crate::syn::Lit;
9+
use proc_macro::{token_stream, Delimiter, Group, Literal, TokenStream, TokenTree};
910

1011
fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
1112
if let Some(TokenTree::Ident(ident)) = it.next() {
@@ -15,21 +16,21 @@ fn try_ident(it: &mut token_stream::IntoIter) -> Option<String> {
1516
}
1617
}
1718

18-
fn try_literal(it: &mut token_stream::IntoIter) -> Option<String> {
19+
fn try_literal(it: &mut token_stream::IntoIter) -> Option<Literal> {
1920
if let Some(TokenTree::Literal(literal)) = it.next() {
20-
Some(literal.to_string())
21+
Some(literal)
2122
} else {
2223
None
2324
}
2425
}
2526

26-
fn try_byte_string(it: &mut token_stream::IntoIter) -> Option<String> {
27-
try_literal(it).and_then(|byte_string| {
28-
if byte_string.starts_with("b\"") && byte_string.ends_with('\"') {
29-
Some(byte_string[2..byte_string.len() - 1].to_string())
30-
} else {
31-
None
27+
fn try_string(it: &mut token_stream::IntoIter) -> Option<String> {
28+
try_literal(it).and_then(|literal| match Lit::new(literal) {
29+
Lit::Str(s) => {
30+
assert!(s.suffix().is_empty(), "Unexpected suffix");
31+
Some(s.value())
3232
}
33+
_ => None,
3334
})
3435
}
3536

@@ -45,7 +46,7 @@ fn expect_punct(it: &mut token_stream::IntoIter) -> char {
4546
}
4647
}
4748

48-
fn expect_literal(it: &mut token_stream::IntoIter) -> String {
49+
fn expect_literal(it: &mut token_stream::IntoIter) -> Literal {
4950
try_literal(it).expect("Expected Literal")
5051
}
5152

@@ -57,8 +58,8 @@ fn expect_group(it: &mut token_stream::IntoIter) -> Group {
5758
}
5859
}
5960

60-
fn expect_byte_string(it: &mut token_stream::IntoIter) -> String {
61-
try_byte_string(it).expect("Expected byte string")
61+
fn expect_string(it: &mut token_stream::IntoIter) -> String {
62+
try_string(it).expect("Expected string")
6263
}
6364

6465
#[derive(Clone, PartialEq)]
@@ -71,7 +72,7 @@ fn expect_array_fields(it: &mut token_stream::IntoIter) -> ParamType {
7172
assert_eq!(expect_punct(it), '<');
7273
let vals = expect_ident(it);
7374
assert_eq!(expect_punct(it), ',');
74-
let max_length_str = expect_literal(it);
75+
let max_length_str = expect_literal(it).to_string();
7576
let max_length = max_length_str
7677
.parse::<usize>()
7778
.expect("Expected usize length");
@@ -102,15 +103,15 @@ fn expect_end(it: &mut token_stream::IntoIter) {
102103
fn get_literal(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
103104
assert_eq!(expect_ident(it), expected_name);
104105
assert_eq!(expect_punct(it), ':');
105-
let literal = expect_literal(it);
106+
let literal = expect_literal(it).to_string();
106107
assert_eq!(expect_punct(it), ',');
107108
literal
108109
}
109110

110-
fn get_byte_string(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
111+
fn get_string(it: &mut token_stream::IntoIter, expected_name: &str) -> String {
111112
assert_eq!(expect_ident(it), expected_name);
112113
assert_eq!(expect_punct(it), ':');
113-
let byte_string = expect_byte_string(it);
114+
let byte_string = expect_string(it);
114115
assert_eq!(expect_punct(it), ',');
115116
byte_string
116117
}
@@ -125,31 +126,31 @@ fn __build_modinfo_string_base(
125126
let string = if builtin {
126127
// Built-in modules prefix their modinfo strings by `module.`.
127128
format!(
128-
"{module}.{field}={content}",
129+
"{module}.{field}={content}\0",
129130
module = module,
130131
field = field,
131132
content = content
132133
)
133134
} else {
134135
// Loadable modules' modinfo strings go as-is.
135-
format!("{field}={content}", field = field, content = content)
136+
format!("{field}={content}\0", field = field, content = content)
136137
};
137138

138139
format!(
139140
"
140141
{cfg}
141142
#[link_section = \".modinfo\"]
142143
#[used]
143-
pub static {variable}: [u8; {length}] = *b\"{string}\\0\";
144+
pub static {variable}: [u8; {length}] = *{string};
144145
",
145146
cfg = if builtin {
146147
"#[cfg(not(MODULE))]"
147148
} else {
148149
"#[cfg(MODULE)]"
149150
},
150151
variable = variable,
151-
length = string.len() + 1,
152-
string = string,
152+
length = string.len(),
153+
string = Literal::byte_string(string.as_bytes()),
153154
)
154155
}
155156

@@ -242,10 +243,14 @@ fn try_simple_param_val(
242243
match param_type {
243244
"bool" => Box::new(|param_it| try_ident(param_it)),
244245
"str" => Box::new(|param_it| {
245-
try_byte_string(param_it)
246-
.map(|s| format!("kernel::module_param::StringParam::Ref(b\"{}\")", s))
246+
try_string(param_it).map(|s| {
247+
format!(
248+
"kernel::module_param::StringParam::Ref({})",
249+
Literal::byte_string(s.as_bytes())
250+
)
251+
})
247252
}),
248-
_ => Box::new(|param_it| try_literal(param_it)),
253+
_ => Box::new(|param_it| try_literal(param_it).map(|x| x.to_string())),
249254
}
250255
}
251256

@@ -349,14 +354,12 @@ impl ModuleInfo {
349354

350355
match key.as_str() {
351356
"type" => info.type_ = expect_ident(it),
352-
"name" => info.name = expect_byte_string(it),
353-
"author" => info.author = Some(expect_byte_string(it)),
354-
"description" => info.description = Some(expect_byte_string(it)),
355-
"license" => info.license = expect_byte_string(it),
356-
"alias" => info.alias = Some(expect_byte_string(it)),
357-
"alias_rtnl_link" => {
358-
info.alias = Some(format!("rtnl-link-{}", expect_byte_string(it)))
359-
}
357+
"name" => info.name = expect_string(it),
358+
"author" => info.author = Some(expect_string(it)),
359+
"description" => info.description = Some(expect_string(it)),
360+
"license" => info.license = expect_string(it),
361+
"alias" => info.alias = Some(expect_string(it)),
362+
"alias_rtnl_link" => info.alias = Some(format!("rtnl-link-{}", expect_string(it))),
360363
"params" => info.params = Some(expect_group(it)),
361364
_ => panic!(
362365
"Unknown key \"{}\". Valid keys are: {:?}.",
@@ -426,7 +429,7 @@ pub fn module(ts: TokenStream) -> TokenStream {
426429
let mut param_it = group.stream().into_iter();
427430
let param_default = get_default(&param_type, &mut param_it);
428431
let param_permissions = get_literal(&mut param_it, "permissions");
429-
let param_description = get_byte_string(&mut param_it, "description");
432+
let param_description = get_string(&mut param_it, "description");
430433
expect_end(&mut param_it);
431434

432435
// TODO: more primitive types
@@ -719,29 +722,29 @@ pub fn module_misc_device(ts: TokenStream) -> TokenStream {
719722
720723
kernel::prelude::module! {{
721724
type: {module},
722-
name: b\"{name}\",
725+
name: {name},
723726
{author}
724727
{description}
725-
license: b\"{license}\",
728+
license: {license},
726729
{alias}
727730
}}
728731
",
729732
module = module,
730733
type_ = info.type_,
731-
name = info.name,
734+
name = Literal::string(&info.name),
732735
author = info
733736
.author
734-
.map(|v| format!("author: b\"{}\",", v))
737+
.map(|v| format!("author: {},", Literal::string(&v)))
735738
.unwrap_or_else(|| "".to_string()),
736739
description = info
737740
.description
738-
.map(|v| format!("description: b\"{}\",", v))
741+
.map(|v| format!("description: {},", Literal::string(&v)))
739742
.unwrap_or_else(|| "".to_string()),
740743
alias = info
741744
.alias
742-
.map(|v| format!("alias: b\"{}\",", v))
745+
.map(|v| format!("alias: {},", Literal::string(&v)))
743746
.unwrap_or_else(|| "".to_string()),
744-
license = info.license
747+
license = Literal::string(&info.license)
745748
)
746749
.parse()
747750
.expect("Error parsing formatted string into token stream.")

samples/rust/rust_chrdev.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use kernel::{c_str, chrdev, file_operations::FileOperations};
1212

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

2121
#[derive(Default)]

samples/rust/rust_minimal.rs

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

1010
module! {
1111
type: RustMinimal,
12-
name: b"rust_minimal",
13-
author: b"Rust for Linux Contributors",
14-
description: b"Rust minimal sample",
15-
license: b"GPL v2",
12+
name: "rust_minimal",
13+
author: "Rust for Linux Contributors",
14+
description: "Rust minimal sample",
15+
license: "GPL v2",
1616
}
1717

1818
struct RustMinimal {

samples/rust/rust_miscdev.rs

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

2121
module! {
2222
type: RustMiscdev,
23-
name: b"rust_miscdev",
24-
author: b"Rust for Linux Contributors",
25-
description: b"Rust miscellaneous device sample",
26-
license: b"GPL v2",
23+
name: "rust_miscdev",
24+
author: "Rust for Linux Contributors",
25+
description: "Rust miscellaneous device sample",
26+
license: "GPL v2",
2727
}
2828

2929
const MAX_TOKENS: usize = 3;

samples/rust/rust_module_parameters.rs

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

1010
module! {
1111
type: RustModuleParameters,
12-
name: b"rust_module_parameters",
13-
author: b"Rust for Linux Contributors",
14-
description: b"Rust module parameters sample",
15-
license: b"GPL v2",
12+
name: "rust_module_parameters",
13+
author: "Rust for Linux Contributors",
14+
description: "Rust module parameters sample",
15+
license: "GPL v2",
1616
params: {
1717
my_bool: bool {
1818
default: true,
1919
permissions: 0,
20-
description: b"Example of bool",
20+
description: "Example of bool",
2121
},
2222
my_i32: i32 {
2323
default: 42,
2424
permissions: 0o644,
25-
description: b"Example of i32",
25+
description: "Example of i32",
2626
},
2727
my_str: str {
28-
default: b"default str val",
28+
default: "default str val",
2929
permissions: 0o644,
30-
description: b"Example of a string param",
30+
description: "Example of a string param",
3131
},
3232
my_usize: usize {
3333
default: 42,
3434
permissions: 0o644,
35-
description: b"Example of usize",
35+
description: "Example of usize",
3636
},
3737
my_array: ArrayParam<i32, 3> {
3838
default: [0, 1],
3939
permissions: 0,
40-
description: b"Example of array",
40+
description: "Example of array",
4141
},
4242
},
4343
}

samples/rust/rust_print.rs

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

1010
module! {
1111
type: RustPrint,
12-
name: b"rust_print",
13-
author: b"Rust for Linux Contributors",
14-
description: b"Rust printing macros sample",
15-
license: b"GPL v2",
12+
name: "rust_print",
13+
author: "Rust for Linux Contributors",
14+
description: "Rust printing macros sample",
15+
license: "GPL v2",
1616
}
1717

1818
struct RustPrint;

0 commit comments

Comments
 (0)