rust_print_main.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. authors: ["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. {
  27. // `Arc` can be used to delegate dynamic dispatch and the following is an example.
  28. // Both `i32` and `&str` implement `Display`. This enables us to express a unified
  29. // behaviour, contract or protocol on both `i32` and `&str` into a single `Arc` of
  30. // type `Arc<dyn Display>`.
  31. use kernel::fmt::Display;
  32. fn arc_dyn_print(arc: &Arc<dyn Display>) {
  33. pr_info!("Arc<dyn Display> says {arc}");
  34. }
  35. let a_i32_display: Arc<dyn Display> = Arc::new(42i32, GFP_KERNEL)?;
  36. let a_str_display: Arc<dyn Display> = a.clone();
  37. arc_dyn_print(&a_i32_display);
  38. arc_dyn_print(&a_str_display);
  39. }
  40. // Pretty-prints the debug formatting with lower-case hexadecimal integers.
  41. pr_info!("{:#x?}", a);
  42. Ok(())
  43. }
  44. impl kernel::Module for RustPrint {
  45. fn init(_module: &'static ThisModule) -> Result<Self> {
  46. pr_info!("Rust printing macros sample (init)\n");
  47. pr_emerg!("Emergency message (level 0) without args\n");
  48. pr_alert!("Alert message (level 1) without args\n");
  49. pr_crit!("Critical message (level 2) without args\n");
  50. pr_err!("Error message (level 3) without args\n");
  51. pr_warn!("Warning message (level 4) without args\n");
  52. pr_notice!("Notice message (level 5) without args\n");
  53. pr_info!("Info message (level 6) without args\n");
  54. pr_info!("A line that");
  55. pr_cont!(" is continued");
  56. pr_cont!(" without args\n");
  57. pr_emerg!("{} message (level {}) with args\n", "Emergency", 0);
  58. pr_alert!("{} message (level {}) with args\n", "Alert", 1);
  59. pr_crit!("{} message (level {}) with args\n", "Critical", 2);
  60. pr_err!("{} message (level {}) with args\n", "Error", 3);
  61. pr_warn!("{} message (level {}) with args\n", "Warning", 4);
  62. pr_notice!("{} message (level {}) with args\n", "Notice", 5);
  63. pr_info!("{} message (level {}) with args\n", "Info", 6);
  64. pr_info!("A {} that", "line");
  65. pr_cont!(" is {}", "continued");
  66. pr_cont!(" with {}\n", "args");
  67. arc_print()?;
  68. trace::trace_rust_sample_loaded(42);
  69. Ok(RustPrint)
  70. }
  71. }
  72. impl Drop for RustPrint {
  73. fn drop(&mut self) {
  74. pr_info!("Rust printing macros sample (exit)\n");
  75. }
  76. }
  77. mod trace {
  78. use kernel::prelude::*;
  79. kernel::declare_trace! {
  80. /// # Safety
  81. ///
  82. /// Always safe to call.
  83. unsafe fn rust_sample_loaded(magic: c_int);
  84. }
  85. pub(crate) fn trace_rust_sample_loaded(magic: i32) {
  86. // SAFETY: Always safe to call.
  87. unsafe { rust_sample_loaded(magic as c_int) }
  88. }
  89. }