Skip to contentSkip to conversation

rust: i2c: prepare for `core::ffi::CStr`

PATCH Applied Miguel Ojeda 2 participants 2 messages Nov 23, 2025

Subsystems (2)
lore
Share & tools
lore

Miguel Ojeda

author maintainer
#0 lore raw

PATCH 1/1 rust: i2c: prepare for `core::ffi::CStr`

The rust-next tree contains commit:

    3b83f5d5e78a ("rust: replace `CStr` with `core::ffi::CStr`")

which, when merged together with commits:

57c5bd9aee94 ("rust: i2c: add basic I2C device and driver abstractions") f3cc26a417b7 ("rust: i2c: add manual I2C device creation abstractions")

from this tree (driver-core), produces errors like the following:

    error[E0599]: no method named `len_with_nul` found for reference `&'static ffi::CStr` in the current scope
      --> rust/kernel/i2c.rs:48:16
       |
    48 |             id.len_with_nul() <= Self::I2C_NAME_SIZE,
       |                ^^^^^^^^^^^^ method not found in `&CStr`

    error[E0599]: no method named `as_bytes_with_nul` found for reference `&'static ffi::CStr` in the current scope
      --> rust/kernel/i2c.rs:51:22
       |
    51 |         let src = id.as_bytes_with_nul();
       |                      ^^^^^^^^^^^^^^^^^
       |
    help: there is a method `to_bytes_with_nul` with a similar name
       |
    51 |         let src = id.to_bytes_with_nul();
       |                      ~~~~~~~~~~~~~~~~~

which were detected in linux-next by Stephen [1].

The i2c code can be independently prepared to be ready for the change, thus do so.

The change is similar to the one done by Tamir in commit 657403637f7d ("rust: acpi: use core::ffi::CStr method names").

---
I see that Stephen added this in his Friday's resolution. However, the CStrExt import does not seem needed as it also comes from the prelude, so that may be removed.

 rust/kernel/i2c.rs | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
rust/kernel/i2c.rs +4 -10
index 95b056cc1a71..491e6cc25cf4 100644
@@ -44,11 +44,8 @@ impl DeviceId {
     /// Create a new device id from an I2C 'id' string.
     #[inline(always)]
     pub const fn new(id: &'static CStr) -> Self {
-        build_assert!(
-            id.len_with_nul() <= Self::I2C_NAME_SIZE,
-            "ID exceeds 20 bytes"
-        );
-        let src = id.as_bytes_with_nul();
+        let src = id.to_bytes_with_nul();
+        build_assert!(src.len() <= Self::I2C_NAME_SIZE, "ID exceeds 20 bytes");
         let mut i2c: bindings::i2c_device_id = pin_init::zeroed();
         let mut i = 0;
         while i < src.len() {
@@ -434,11 +431,8 @@ impl I2cBoardInfo {
     /// Create a new [`I2cBoardInfo`] for a kernel driver.
     #[inline(always)]
     pub const fn new(type_: &'static CStr, addr: u16) -> Self {
-        build_assert!(
-            type_.len_with_nul() <= Self::I2C_TYPE_SIZE,
-            "Type exceeds 20 bytes"
-        );
-        let src = type_.as_bytes_with_nul();
+        let src = type_.to_bytes_with_nul();
+        build_assert!(src.len() <= Self::I2C_TYPE_SIZE, "Type exceeds 20 bytes");
         let mut i2c_board_info: bindings::i2c_board_info = pin_init::zeroed();
         let mut i: usize = 0;
         while i < src.len() {

base-commit: 4635406417bb842862d6322c461743c9b128c444 --
2.52.0

reviewer Applied
#1 lore raw
Quoted 30 lines · Miguel Ojeda

The rust-next tree contains commit:

    3b83f5d5e78a ("rust: replace `CStr` with `core::ffi::CStr`")

which, when merged together with commits:

57c5bd9aee94 ("rust: i2c: add basic I2C device and driver abstractions") f3cc26a417b7 ("rust: i2c: add manual I2C device creation abstractions")

from this tree (driver-core), produces errors like the following:

    error[E0599]: no method named `len_with_nul` found for reference `&'static ffi::CStr` in the current scope
      --> rust/kernel/i2c.rs:48:16
       |
    48 |             id.len_with_nul() <= Self::I2C_NAME_SIZE,
       |                ^^^^^^^^^^^^ method not found in `&CStr`

    error[E0599]: no method named `as_bytes_with_nul` found for reference `&'static ffi::CStr` in the current scope
      --> rust/kernel/i2c.rs:51:22
       |
    51 |         let src = id.as_bytes_with_nul();
       |                      ^^^^^^^^^^^^^^^^^
       |
    help: there is a method `to_bytes_with_nul` with a similar name
       |
    51 |         let src = id.to_bytes_with_nul();
       |                      ~~~~~~~~~~~~~~~~~

which were detected in linux-next by Stephen [1].

Quoted 8 lines

The i2c code can be independently prepared to be ready for the change, thus do so.

The change is similar to the one done by Tamir in commit 657403637f7d ("rust: acpi: use core::ffi::CStr method names").

Applied to driver-core-next, thanks!