[PATCH v2 08/21] rust: error: impl From<FromBytesWithNulError> for Kernel Error

From: alistair23

Date: Tue Jun 23 2026 - 00:56:15 EST


From: Alistair Francis <alistair.francis@xxxxxxx>

Implement From<FromBytesWithNulError> for the Kernel Error type as we
will use it in a future patch.

As this is now generally available we can remove it from the test case
as well.

Signed-off-by: Alistair Francis <alistair.francis@xxxxxxx>
---
rust/kernel/error.rs | 18 +++++++++++++++---
rust/kernel/str.rs | 7 -------
2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index a56ba6309594..1a7221fe5682 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -12,9 +12,14 @@
str::CStr,
};

-use core::num::NonZeroI32;
-use core::num::TryFromIntError;
-use core::str::Utf8Error;
+use core::{
+ ffi::FromBytesWithNulError,
+ num::{
+ NonZeroI32,
+ TryFromIntError, //
+ },
+ str::Utf8Error, //
+};

/// Contains the C-compatible error codes.
#[rustfmt::skip]
@@ -256,6 +261,13 @@ fn from(e: core::convert::Infallible) -> Error {
}
}

+impl From<FromBytesWithNulError> for Error {
+ #[inline]
+ fn from(_: FromBytesWithNulError) -> Error {
+ code::EINVAL
+ }
+}
+
/// A [`Result`] with an [`Error`] error type.
///
/// To be used as the return type for functions that may fail.
diff --git a/rust/kernel/str.rs b/rust/kernel/str.rs
index b3caa9a1c898..a556788bcc5e 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -433,13 +433,6 @@ macro_rules! c_str {
mod tests {
use super::*;

- impl From<core::ffi::FromBytesWithNulError> for Error {
- #[inline]
- fn from(_: core::ffi::FromBytesWithNulError) -> Error {
- EINVAL
- }
- }
-
macro_rules! format {
($($f:tt)*) => ({
CString::try_from_fmt(fmt!($($f)*))?.to_str()?
--
2.54.0