rust_print.rs 2.3 KB

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