Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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").

Link: https://lore.kernel.org/all/20251120181111.65ce75a0@canb.auug.org.au/ [1]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://patch.msgid.link/20251123163536.1771801-1-ojeda@kernel.org
Signed-off-by: Danilo Krummrich <dakr@kernel.org>

authored by

Miguel Ojeda and committed by
Danilo Krummrich
a762f883 46354064

+4 -10
+4 -10
rust/kernel/i2c.rs
··· 44 44 /// Create a new device id from an I2C 'id' string. 45 45 #[inline(always)] 46 46 pub const fn new(id: &'static CStr) -> Self { 47 - build_assert!( 48 - id.len_with_nul() <= Self::I2C_NAME_SIZE, 49 - "ID exceeds 20 bytes" 50 - ); 51 - let src = id.as_bytes_with_nul(); 47 + let src = id.to_bytes_with_nul(); 48 + build_assert!(src.len() <= Self::I2C_NAME_SIZE, "ID exceeds 20 bytes"); 52 49 let mut i2c: bindings::i2c_device_id = pin_init::zeroed(); 53 50 let mut i = 0; 54 51 while i < src.len() { ··· 431 434 /// Create a new [`I2cBoardInfo`] for a kernel driver. 432 435 #[inline(always)] 433 436 pub const fn new(type_: &'static CStr, addr: u16) -> Self { 434 - build_assert!( 435 - type_.len_with_nul() <= Self::I2C_TYPE_SIZE, 436 - "Type exceeds 20 bytes" 437 - ); 438 - let src = type_.as_bytes_with_nul(); 437 + let src = type_.to_bytes_with_nul(); 438 + build_assert!(src.len() <= Self::I2C_TYPE_SIZE, "Type exceeds 20 bytes"); 439 439 let mut i2c_board_info: bindings::i2c_board_info = pin_init::zeroed(); 440 440 let mut i: usize = 0; 441 441 while i < src.len() {