Skip to content

Commit f431c5c

Browse files
fbqojeda
authored andcommitted
samples: rust: print: Add sample code for Arc printing
This both demonstrates the usage of different print format in Rust and serves as a selftest for the `Display` and `Debug` implementation of `Arc` and its friends. Signed-off-by: Boqun Feng <[email protected]> Reviewed-by: Björn Roy Baron <[email protected]> Reviewed-by: Finn Behrens <[email protected]> Reviewed-by: Vincenzo Palazzo <[email protected]> Reviewed-by: Gary Guo <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Applied suggestions and reworded for fixing title typos. ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 00140a8 commit f431c5c

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

samples/rust/rust_print.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@ module! {
1515

1616
struct RustPrint;
1717

18+
fn arc_print() -> Result {
19+
use kernel::sync::*;
20+
21+
let a = Arc::try_new(1)?;
22+
let b = UniqueArc::try_new("hello, world")?;
23+
24+
// Prints the value of data in `a`.
25+
pr_info!("{}", a);
26+
27+
// Uses ":?" to print debug fmt of `b`.
28+
pr_info!("{:?}", b);
29+
30+
let a: Arc<&str> = b.into();
31+
let c = a.clone();
32+
33+
// Uses `dbg` to print, will move `c` (for temporary debugging purposes).
34+
dbg!(c);
35+
36+
// Pretty-prints the debug formatting with lower-case hexadecimal integers.
37+
pr_info!("{:#x?}", a);
38+
39+
Ok(())
40+
}
41+
1842
impl kernel::Module for RustPrint {
1943
fn init(_module: &'static ThisModule) -> Result<Self> {
2044
pr_info!("Rust printing macros sample (init)\n");
@@ -43,6 +67,8 @@ impl kernel::Module for RustPrint {
4367
pr_cont!(" is {}", "continued");
4468
pr_cont!(" with {}\n", "args");
4569

70+
arc_print()?;
71+
4672
Ok(RustPrint)
4773
}
4874
}

0 commit comments

Comments
 (0)