Skip to contentSkip to conversation

rust: replace `kernel::c_str!` with C-Strings in seq_file and device

PATCH In review Ryan Foster 3 participants 6 messages Jan 20

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: replace `kernel::c_str!` with C-Strings in seq_file and device

C-String literals were added in Rust 1.77. Replace instances of kernel::c_str! with C-String literals where possible.

This patch updates seq_file and device modules to use the native C-string literal syntax (c"...") instead of the kernel::c_str! macro.

---
 rust/kernel/device.rs   | 5 +----
 rust/kernel/seq_file.rs | 4 ++--
 2 files changed, 3 insertions(+), 6 deletions(-)
rust/kernel/device.rs +1 -4
index 71b200df0f40..1c3d1d962d15 100644
@@ -12,9 +12,6 @@
 };
 use core::{any::TypeId, marker::PhantomData, ptr};
 
-#[cfg(CONFIG_PRINTK)]
-use crate::c_str;
-
 pub mod property;
 
 // Assert that we can `read()` / `write()` a `TypeId` instance from / into `struct driver_type`.
@@ -462,7 +459,7 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
             bindings::_dev_printk(
                 klevel.as_ptr().cast::<crate::ffi::c_char>(),
                 self.as_raw(),
-                c_str!("%pA").as_char_ptr(),
+                c"%pA".as_char_ptr(),
                 core::ptr::from_ref(&msg).cast::<crate::ffi::c_void>(),
             )
         };
rust/kernel/seq_file.rs +2 -2
index 855e533813a6..518265558d66 100644
@@ -4,7 +4,7 @@
 //!
 //! C header: [`include/linux/seq_file.h`](srctree/include/linux/seq_file.h)
 
-use crate::{bindings, c_str, fmt, str::CStrExt as _, types::NotThreadSafe, types::Opaque};
+use crate::{bindings, fmt, str::CStrExt as _, types::NotThreadSafe, types::Opaque};
 
 /// A utility for generating the contents of a seq file.
 #[repr(transparent)]
@@ -36,7 +36,7 @@ pub fn call_printf(&self, args: fmt::Arguments<'_>) {
         unsafe {
             bindings::seq_printf(
                 self.inner.get(),
-                c_str!("%pA").as_char_ptr(),
+                c"%pA".as_char_ptr(),
                 core::ptr::from_ref(&args).cast::<crate::ffi::c_void>(),
             );
         }
Signature · 1 line
2.52.0
reviewer
#1 lore raw
Quoted 40 lines · Ryan Foster

C-String literals were added in Rust 1.77. Replace instances of kernel::c_str! with C-String literals where possible.

This patch updates seq_file and device modules to use the native C-string literal syntax (c"...") instead of the kernel::c_str! macro.

---
 rust/kernel/device.rs   | 5 +----
 rust/kernel/seq_file.rs | 4 ++--
 2 files changed, 3 insertions(+), 6 deletions(-)
rust/kernel/device.rs +1 -4
index 71b200df0f40..1c3d1d962d15 100644
@@ -12,9 +12,6 @@
 };
 use core::{any::TypeId, marker::PhantomData, ptr};
 
-#[cfg(CONFIG_PRINTK)]
-use crate::c_str;
-
 pub mod property;
 
 // Assert that we can `read()` / `write()` a `TypeId` instance from / into `struct driver_type`.
@@ -462,7 +459,7 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
             bindings::_dev_printk(
                 klevel.as_ptr().cast::<crate::ffi::c_char>(),
                 self.as_raw(),
-                c_str!("%pA").as_char_ptr(),
+                c"%pA".as_char_ptr(),
                 core::ptr::from_ref(&msg).cast::<crate::ffi::c_void>(),
             )
         };
rust/kernel/seq_file.rs +0 -0
index 855e533813a6..518265558d66 100644

rust/kernel/seq_file.rs line 7

Quoted 5 lines
rust/kernel/seq_file.rs +1 -1
@@ -4,7 +4,7 @@
 //!
 //! C header: [`include/linux/seq_file.h`](srctree/include/linux/seq_file.h)
 
-use crate::{bindings, c_str, fmt, str::CStrExt as _, types::NotThreadSafe, types::Opaque};
+use crate::{bindings, fmt, str::CStrExt as _, types::NotThreadSafe, types::Opaque};

As you're changing the import list, can you also convert it to the new kernel import style?

Best,
Gary

Quoted 12 lines
rust/kernel/seq_file.rs +1 -1
 /// A utility for generating the contents of a seq file.
 #[repr(transparent)]
@@ -36,7 +36,7 @@ pub fn call_printf(&self, args: fmt::Arguments<'_>) {
         unsafe {
             bindings::seq_printf(
                 self.inner.get(),
-                c_str!("%pA").as_char_ptr(),
+                c"%pA".as_char_ptr(),
                 core::ptr::from_ref(&args).cast::<crate::ffi::c_void>(),
             );
         }
#2 lore raw

PATCH 1/1 rust: replace `kernel::c_str!` with C-Strings in seq_file and device

C-String literals were added in Rust 1.77. Replace instances of kernel::c_str! with C-String literals where possible.

This patch updates seq_file and device modules to use the native C-string literal syntax (c"...") instead of the kernel::c_str! macro.

While at it, convert imports to the kernel vertical import style.

---
 rust/kernel/device.rs   | 19 ++++++++++++-------
 rust/kernel/seq_file.rs | 12 ++++++++++--
 2 files changed, 22 insertions(+), 9 deletions(-)
rust/kernel/device.rs +12 -7
index 71b200df0f40..a87350c1a67d 100644
@@ -5,15 +5,20 @@
 //! C header: [`include/linux/device.h`](srctree/include/linux/device.h)
 
 use crate::{
-    bindings, fmt,
+    bindings,
+    fmt,
     prelude::*,
     sync::aref::ARef,
-    types::{ForeignOwnable, Opaque},
+    types::{
+        ForeignOwnable,
+        Opaque, //
+    }, //
+};
+use core::{
+    any::TypeId,
+    marker::PhantomData,
+    ptr, //
 };
-use core::{any::TypeId, marker::PhantomData, ptr};
-
-#[cfg(CONFIG_PRINTK)]
-use crate::c_str;
 
 pub mod property;
 
@@ -462,7 +467,7 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
             bindings::_dev_printk(
                 klevel.as_ptr().cast::<crate::ffi::c_char>(),
                 self.as_raw(),
-                c_str!("%pA").as_char_ptr(),
+                c"%pA".as_char_ptr(),
                 core::ptr::from_ref(&msg).cast::<crate::ffi::c_void>(),
             )
         };
rust/kernel/seq_file.rs +10 -2
index 855e533813a6..109ad6670907 100644
@@ -4,7 +4,15 @@
 //!
 //! C header: [`include/linux/seq_file.h`](srctree/include/linux/seq_file.h)
 
-use crate::{bindings, c_str, fmt, str::CStrExt as _, types::NotThreadSafe, types::Opaque};
+use crate::{
+    bindings,
+    fmt,
+    str::CStrExt as _,
+    types::{
+        NotThreadSafe,
+        Opaque, //
+    }, //
+};
 
 /// A utility for generating the contents of a seq file.
 #[repr(transparent)]
@@ -36,7 +44,7 @@ pub fn call_printf(&self, args: fmt::Arguments<'_>) {
         unsafe {
             bindings::seq_printf(
                 self.inner.get(),
-                c_str!("%pA").as_char_ptr(),
+                c"%pA".as_char_ptr(),
                 core::ptr::from_ref(&args).cast::<crate::ffi::c_void>(),
             );
         }
Signature · 1 line
2.52.0
reviewer Reviewed-by on v1 1/1
#3 lore raw
Quoted 9 lines · Ryan Foster

C-String literals were added in Rust 1.77. Replace instances of kernel::c_str! with C-String literals where possible.

This patch updates seq_file and device modules to use the native C-string literal syntax (c"...") instead of the kernel::c_str! macro.

While at it, convert imports to the kernel vertical import style.

Quoted 4 lines
---
 rust/kernel/device.rs   | 19 ++++++++++++-------
 rust/kernel/seq_file.rs | 12 ++++++++++--
 2 files changed, 22 insertions(+), 9 deletions(-)
#4 lore raw
Quoted 5 lines · Gary Guo
Quoted 3 lines · Ryan Foster

C-String literals were added in Rust 1.77. Replace instances of kernel::c_str! with C-String literals where possible.

Quoted 8 lines
Quoted 6 lines

This patch updates seq_file and device modules to use the native C-string literal syntax (c"...") instead of the kernel::c_str! macro.

While at it, convert imports to the kernel vertical import style.

device.rs was done in
https://lore.kernel.org/all/[email protected]/
and seq_file.rs is waiting for a maintainer's ack in
https://lore.kernel.org/all/[email protected]/.

#5 lore raw
In reply to Gary Guo

Hi Gary,
Thi is now fixed, great catch thanks.

Best ,Ryan

Quoted 67 lines · Gary Guo
Quoted 40 lines · Ryan Foster

C-String literals were added in Rust 1.77. Replace instances of kernel::c_str! with C-String literals where possible.

This patch updates seq_file and device modules to use the native C-string literal syntax (c"...") instead of the kernel::c_str! macro.

---
 rust/kernel/device.rs   | 5 +----
 rust/kernel/seq_file.rs | 4 ++--
 2 files changed, 3 insertions(+), 6 deletions(-)
rust/kernel/device.rs +1 -4
index 71b200df0f40..1c3d1d962d15 100644
@@ -12,9 +12,6 @@
 };
 use core::{any::TypeId, marker::PhantomData, ptr};
 
-#[cfg(CONFIG_PRINTK)]
-use crate::c_str;
-
 pub mod property;
 
 // Assert that we can `read()` / `write()` a `TypeId` instance from / into `struct driver_type`.
@@ -462,7 +459,7 @@ unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
             bindings::_dev_printk(
                 klevel.as_ptr().cast::<crate::ffi::c_char>(),
                 self.as_raw(),
-                c_str!("%pA").as_char_ptr(),
+                c"%pA".as_char_ptr(),
                 core::ptr::from_ref(&msg).cast::<crate::ffi::c_void>(),
             )
         };
rust/kernel/seq_file.rs +0 -0
index 855e533813a6..518265558d66 100644

rust/kernel/seq_file.rs line 7

Quoted 5 lines
rust/kernel/seq_file.rs +1 -1
@@ -4,7 +4,7 @@
 //!
 //! C header: [`include/linux/seq_file.h`](srctree/include/linux/seq_file.h)
 
-use crate::{bindings, c_str, fmt, str::CStrExt as _, types::NotThreadSafe, types::Opaque};
+use crate::{bindings, fmt, str::CStrExt as _, types::NotThreadSafe, types::Opaque};

As you're changing the import list, can you also convert it to the new kernel import style?

Best,
Gary

Quoted 12 lines
rust/kernel/seq_file.rs +1 -1
 /// A utility for generating the contents of a seq file.
 #[repr(transparent)]
@@ -36,7 +36,7 @@ pub fn call_printf(&self, args: fmt::Arguments<'_>) {
         unsafe {
             bindings::seq_printf(
                 self.inner.get(),
-                c_str!("%pA").as_char_ptr(),
+                c"%pA".as_char_ptr(),
                 core::ptr::from_ref(&args).cast::<crate::ffi::c_void>(),
             );
         }