[PATCH 06/18] rust: error: impl From<FromBytesWithNulError> for Kernel Error

From: alistair23

Date: Thu May 07 2026 - 23:20:24 EST


From: Alistair Francis <alistair.francis@xxxxxxx>

Implement From<FromBytesWithNulError> for the Kernel Error type

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

diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 05cf869ac090..5463addbfd95 100644
--- a/rust/kernel/error.rs
+++ b/rust/kernel/error.rs
@@ -12,6 +12,7 @@
str::CStr,
};

+use core::ffi::FromBytesWithNulError;
use core::num::NonZeroI32;
use core::num::TryFromIntError;
use core::str::Utf8Error;
@@ -258,6 +259,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 8311d91549e1..17070dde0e35 100644
--- a/rust/kernel/str.rs
+++ b/rust/kernel/str.rs
@@ -419,13 +419,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.52.0