Skip to content

Commit c3630df

Browse files
committed
rust: samples: add rust_print example
Add example to exercise the printing macros (`pr_*!`) introduced in the previous patches. Reviewed-by: Finn Behrens <[email protected]> Reviewed-by: Wei Liu <[email protected]> Tested-by: Sergio González Collado <[email protected]> Signed-off-by: Miguel Ojeda <[email protected]>
1 parent fc6c7ca commit c3630df

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

samples/rust/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ config SAMPLE_RUST_MINIMAL
2020

2121
If unsure, say N.
2222

23+
config SAMPLE_RUST_PRINT
24+
tristate "Printing macros"
25+
help
26+
This option builds the Rust printing macros sample.
27+
28+
To compile this as a module, choose M here:
29+
the module will be called rust_print.
30+
31+
If unsure, say N.
32+
2333
config SAMPLE_RUST_HOSTPROGS
2434
bool "Host programs"
2535
help

samples/rust/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-2.0
22

33
obj-$(CONFIG_SAMPLE_RUST_MINIMAL) += rust_minimal.o
4+
obj-$(CONFIG_SAMPLE_RUST_PRINT) += rust_print.o
45

56
subdir-$(CONFIG_SAMPLE_RUST_HOSTPROGS) += hostprogs

samples/rust/rust_print.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
//! Rust printing macros sample.
4+
5+
use kernel::pr_cont;
6+
use kernel::prelude::*;
7+
8+
module! {
9+
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",
14+
}
15+
16+
struct RustPrint;
17+
18+
impl kernel::Module for RustPrint {
19+
fn init(_module: &'static ThisModule) -> Result<Self> {
20+
pr_info!("Rust printing macros sample (init)\n");
21+
22+
pr_emerg!("Emergency message (level 0) without args\n");
23+
pr_alert!("Alert message (level 1) without args\n");
24+
pr_crit!("Critical message (level 2) without args\n");
25+
pr_err!("Error message (level 3) without args\n");
26+
pr_warn!("Warning message (level 4) without args\n");
27+
pr_notice!("Notice message (level 5) without args\n");
28+
pr_info!("Info message (level 6) without args\n");
29+
30+
pr_info!("A line that");
31+
pr_cont!(" is continued");
32+
pr_cont!(" without args\n");
33+
34+
pr_emerg!("{} message (level {}) with args\n", "Emergency", 0);
35+
pr_alert!("{} message (level {}) with args\n", "Alert", 1);
36+
pr_crit!("{} message (level {}) with args\n", "Critical", 2);
37+
pr_err!("{} message (level {}) with args\n", "Error", 3);
38+
pr_warn!("{} message (level {}) with args\n", "Warning", 4);
39+
pr_notice!("{} message (level {}) with args\n", "Notice", 5);
40+
pr_info!("{} message (level {}) with args\n", "Info", 6);
41+
42+
pr_info!("A {} that", "line");
43+
pr_cont!(" is {}", "continued");
44+
pr_cont!(" with {}\n", "args");
45+
46+
Ok(RustPrint)
47+
}
48+
}
49+
50+
impl Drop for RustPrint {
51+
fn drop(&mut self) {
52+
pr_info!("Rust printing macros sample (exit)\n");
53+
}
54+
}

0 commit comments

Comments
 (0)