162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci//! Rust printing macros sample.
462306a36Sopenharmony_ci
562306a36Sopenharmony_ciuse kernel::pr_cont;
662306a36Sopenharmony_ciuse kernel::prelude::*;
762306a36Sopenharmony_ci
862306a36Sopenharmony_cimodule! {
962306a36Sopenharmony_ci    type: RustPrint,
1062306a36Sopenharmony_ci    name: "rust_print",
1162306a36Sopenharmony_ci    author: "Rust for Linux Contributors",
1262306a36Sopenharmony_ci    description: "Rust printing macros sample",
1362306a36Sopenharmony_ci    license: "GPL",
1462306a36Sopenharmony_ci}
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistruct RustPrint;
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cifn arc_print() -> Result {
1962306a36Sopenharmony_ci    use kernel::sync::*;
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci    let a = Arc::try_new(1)?;
2262306a36Sopenharmony_ci    let b = UniqueArc::try_new("hello, world")?;
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci    // Prints the value of data in `a`.
2562306a36Sopenharmony_ci    pr_info!("{}", a);
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci    // Uses ":?" to print debug fmt of `b`.
2862306a36Sopenharmony_ci    pr_info!("{:?}", b);
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci    let a: Arc<&str> = b.into();
3162306a36Sopenharmony_ci    let c = a.clone();
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci    // Uses `dbg` to print, will move `c` (for temporary debugging purposes).
3462306a36Sopenharmony_ci    dbg!(c);
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci    // Pretty-prints the debug formatting with lower-case hexadecimal integers.
3762306a36Sopenharmony_ci    pr_info!("{:#x?}", a);
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci    Ok(())
4062306a36Sopenharmony_ci}
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciimpl kernel::Module for RustPrint {
4362306a36Sopenharmony_ci    fn init(_module: &'static ThisModule) -> Result<Self> {
4462306a36Sopenharmony_ci        pr_info!("Rust printing macros sample (init)\n");
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci        pr_emerg!("Emergency message (level 0) without args\n");
4762306a36Sopenharmony_ci        pr_alert!("Alert message (level 1) without args\n");
4862306a36Sopenharmony_ci        pr_crit!("Critical message (level 2) without args\n");
4962306a36Sopenharmony_ci        pr_err!("Error message (level 3) without args\n");
5062306a36Sopenharmony_ci        pr_warn!("Warning message (level 4) without args\n");
5162306a36Sopenharmony_ci        pr_notice!("Notice message (level 5) without args\n");
5262306a36Sopenharmony_ci        pr_info!("Info message (level 6) without args\n");
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci        pr_info!("A line that");
5562306a36Sopenharmony_ci        pr_cont!(" is continued");
5662306a36Sopenharmony_ci        pr_cont!(" without args\n");
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci        pr_emerg!("{} message (level {}) with args\n", "Emergency", 0);
5962306a36Sopenharmony_ci        pr_alert!("{} message (level {}) with args\n", "Alert", 1);
6062306a36Sopenharmony_ci        pr_crit!("{} message (level {}) with args\n", "Critical", 2);
6162306a36Sopenharmony_ci        pr_err!("{} message (level {}) with args\n", "Error", 3);
6262306a36Sopenharmony_ci        pr_warn!("{} message (level {}) with args\n", "Warning", 4);
6362306a36Sopenharmony_ci        pr_notice!("{} message (level {}) with args\n", "Notice", 5);
6462306a36Sopenharmony_ci        pr_info!("{} message (level {}) with args\n", "Info", 6);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci        pr_info!("A {} that", "line");
6762306a36Sopenharmony_ci        pr_cont!(" is {}", "continued");
6862306a36Sopenharmony_ci        pr_cont!(" with {}\n", "args");
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci        arc_print()?;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci        Ok(RustPrint)
7362306a36Sopenharmony_ci    }
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ciimpl Drop for RustPrint {
7762306a36Sopenharmony_ci    fn drop(&mut self) {
7862306a36Sopenharmony_ci        pr_info!("Rust printing macros sample (exit)\n");
7962306a36Sopenharmony_ci    }
8062306a36Sopenharmony_ci}
81