Commit f431c5c5 authored by Boqun Feng's avatar Boqun Feng Committed by Miguel Ojeda
Browse files

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: default avatarBoqun Feng <boqun.feng@gmail.com>
Reviewed-by: default avatarBjörn Roy Baron <bjorn3_gh@protonmail.com>
Reviewed-by: default avatarFinn Behrens <fin@nyantec.com>
Reviewed-by: default avatarVincenzo Palazzo <vincenzopalazzodev@gmail.com>
Reviewed-by: default avatarGary Guo <gary@garyguo.net>
Reviewed-by: default avatarAndreas Hindborg <a.hindborg@samsung.com>
Link: https://lore.kernel.org/r/20230207185216.1314638-3-boqun.feng@gmail.com


[ Applied suggestions and reworded for fixing title typos. ]
Signed-off-by: default avatarMiguel Ojeda <ojeda@kernel.org>
parent 00140a83
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -15,6 +15,30 @@

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 @@ fn init(_module: &'static ThisModule) -> Result<Self> {
        pr_cont!(" is {}", "continued");
        pr_cont!(" with {}\n", "args");

        arc_print()?;

        Ok(RustPrint)
    }
}