rust_print.rs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! Rust printing macros sample.
  3. use kernel::pr_cont;
  4. use kernel::prelude::*;
  5. module! {
  6. type: RustPrint,
  7. name: "rust_print",
  8. author: "Rust for Linux Contributors",
  9. description: "Rust printing macros sample",
  10. license: "GPL",
  11. }
  12. struct RustPrint;
  13. fn arc_print() -> Result {
  14. use kernel::sync::*;
  15. let a = Arc::new(1, GFP_KERNEL)?;
  16. let b = UniqueArc::new("hello, world", GFP_KERNEL)?;
  17. // Prints the value of data in `a`.
  18. pr_info!("{}", a);
  19. // Uses ":?" to print debug fmt of `b`.
  20. pr_info!("{:?}", b);
  21. let a: Arc<&str> = b.into();
  22. let c = a.clone();
  23. // Uses `dbg` to print, will move `c` (for temporary debugging purposes).
  24. dbg!(c);
  25. // Pretty-prints the debug formatting with lower-case hexadecimal integers.
  26. pr_info!("{:#x?}", a);
  27. Ok(())
  28. }
  29. impl kernel::Module for RustPrint {
  30. fn init(_module: &'static ThisModule) -> Result<Self> {
  31. pr_info!("Rust printing macros sample (init)\n");
  32. pr_emerg!("Emergency message (level 0) without args\n");
  33. pr_alert!("Alert message (level 1) without args\n");
  34. pr_crit!("Critical message (level 2) without args\n");
  35. pr_err!("Error message (level 3) without args\n");
  36. pr_warn!("Warning message (level 4) without args\n");
  37. pr_notice!("Notice message (level 5) without args\n");
  38. pr_info!("Info message (level 6) without args\n");
  39. pr_info!("A line that");
  40. pr_cont!(" is continued");
  41. pr_cont!(" without args\n");
  42. pr_emerg!("{} message (level {}) with args\n", "Emergency", 0);
  43. pr_alert!("{} message (level {}) with args\n", "Alert", 1);
  44. pr_crit!("{} message (level {}) with args\n", "Critical", 2);
  45. pr_err!("{} message (level {}) with args\n", "Error", 3);
  46. pr_warn!("{} message (level {}) with args\n", "Warning", 4);
  47. pr_notice!("{} message (level {}) with args\n", "Notice", 5);
  48. pr_info!("{} message (level {}) with args\n", "Info", 6);
  49. pr_info!("A {} that", "line");
  50. pr_cont!(" is {}", "continued");
  51. pr_cont!(" with {}\n", "args");
  52. arc_print()?;
  53. Ok(RustPrint)
  54. }
  55. }
  56. impl Drop for RustPrint {
  57. fn drop(&mut self) {
  58. pr_info!("Rust printing macros sample (exit)\n");
  59. }
  60. }