Skip to content

Commit 228e547

Browse files
bonzinieli-schwartz
authored andcommitted
modernize Rust template
Signed-off-by: Paolo Bonzini <[email protected]> (cherry picked from commit 8578a99)
1 parent a686faa commit 228e547

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

mesonbuild/templates/rusttemplates.py

+15-12
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,28 @@
2020
pub fn {function_name}() -> i32 {{
2121
return internal_function();
2222
}}
23-
'''
2423
25-
lib_rust_test_template = '''extern crate {crate_file};
24+
#[cfg(test)]
25+
mod tests {{
26+
use super::*;
2627
27-
fn main() {{
28-
println!("printing: {{}}", {crate_file}::{function_name}());
28+
#[test]
29+
fn test_function() {{
30+
assert_eq!({function_name}(), 0);
31+
}}
2932
}}
3033
'''
3134

3235

3336
lib_rust_meson_template = '''project('{project_name}', 'rust',
34-
version : '{version}',
35-
default_options : ['warning_level=3'])
37+
version : '{version}', meson_version: '>=1.3.0',
38+
default_options : ['rust_std=2021', 'warning_level=3'])
39+
40+
rust = import('rust')
3641
3742
shlib = static_library('{lib_name}', '{source_file}', install : true)
3843
39-
test_exe = executable('{test_exe_name}', '{test_source_file}',
40-
link_with : shlib)
41-
test('{test_name}', test_exe)
44+
rust.test('{test_name}', shlib)
4245
4346
# Make this library usable as a Meson subproject.
4447
{ltoken}_dep = declare_dependency(
@@ -54,8 +57,8 @@
5457
'''
5558

5659
hello_rust_meson_template = '''project('{project_name}', 'rust',
57-
version : '{version}',
58-
default_options : ['warning_level=3'])
60+
version : '{version}', meson_version: '>=1.3.0',
61+
default_options : ['rust_std=2021', 'warning_level=3'])
5962
6063
exe = executable('{exe_name}', '{source_name}',
6164
install : true)
@@ -70,7 +73,7 @@ class RustProject(FileImpl):
7073
exe_template = hello_rust_template
7174
exe_meson_template = hello_rust_meson_template
7275
lib_template = lib_rust_template
73-
lib_test_template = lib_rust_test_template
76+
lib_test_template = None
7477
lib_meson_template = lib_rust_meson_template
7578

7679
def lib_kwargs(self) -> T.Dict[str, str]:

mesonbuild/templates/sampleimpl.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def lib_template(self) -> str:
4141
pass
4242

4343
@abc.abstractproperty
44-
def lib_test_template(self) -> str:
44+
def lib_test_template(self) -> T.Optional[str]:
4545
pass
4646

4747
@abc.abstractproperty
@@ -85,8 +85,9 @@ def create_library(self) -> None:
8585
}
8686
with open(lib_name, 'w', encoding='utf-8') as f:
8787
f.write(self.lib_template.format(**kwargs))
88-
with open(test_name, 'w', encoding='utf-8') as f:
89-
f.write(self.lib_test_template.format(**kwargs))
88+
if self.lib_test_template:
89+
with open(test_name, 'w', encoding='utf-8') as f:
90+
f.write(self.lib_test_template.format(**kwargs))
9091
with open('meson.build', 'w', encoding='utf-8') as f:
9192
f.write(self.lib_meson_template.format(**kwargs))
9293

@@ -132,8 +133,9 @@ def create_library(self) -> None:
132133
kwargs = self.lib_kwargs()
133134
with open(lib_name, 'w', encoding='utf-8') as f:
134135
f.write(self.lib_template.format(**kwargs))
135-
with open(test_name, 'w', encoding='utf-8') as f:
136-
f.write(self.lib_test_template.format(**kwargs))
136+
if self.lib_test_template:
137+
with open(test_name, 'w', encoding='utf-8') as f:
138+
f.write(self.lib_test_template.format(**kwargs))
137139
with open('meson.build', 'w', encoding='utf-8') as f:
138140
f.write(self.lib_meson_template.format(**kwargs))
139141

0 commit comments

Comments
 (0)