mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/xiang/erofs.git
synced 2026-10-28 12:53:14 +01:00
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]>
This commit is contained in:
1 parent
00140a8308
commit
f431c5c581
1 file changed
+26
@@ -15,6 +15,30 @@ module! {
|
||||
|
||||
struct RustPrint;
|
||||
|
||||
fn arc_print() -> Result {
|
||||
use kernel::sync::*;
|
||||
|
||||
let a = Arc::try_new(1)?;
|
||||
let b = UniqueArc::try_new("hello, world")?;
|
||||
|
||||
// Prints the value of data in `a`.
|
||||
pr_info!("{}", a);
|
||||
|
||||
// Uses ":?" to print debug fmt of `b`.
|
||||
pr_info!("{:?}", b);
|
||||
|
||||
let a: Arc<&str> = b.into();
|
||||
let c = a.clone();
|
||||
|
||||
// Uses `dbg` to print, will move `c` (for temporary debugging purposes).
|
||||
dbg!(c);
|
||||
|
||||
// Pretty-prints the debug formatting with lower-case hexadecimal integers.
|
||||
pr_info!("{:#x?}", a);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
impl kernel::Module for RustPrint {
|
||||
fn init(_module: &'static ThisModule) -> Result<Self> {
|
||||
pr_info!("Rust printing macros sample (init)\n");
|
||||
@@ -43,6 +67,8 @@ impl kernel::Module for RustPrint {
|
||||
pr_cont!(" is {}", "continued");
|
||||
pr_cont!(" with {}\n", "args");
|
||||
|
||||
arc_print()?;
|
||||
|
||||
Ok(RustPrint)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user