[RFC v3 07/27] rust: error: impl From<FromBytesWithNulError> for Kernel Error

alistair23@gmail.com posted 27 patches 1 month, 2 weeks ago
[RFC v3 07/27] rust: error: impl From<FromBytesWithNulError> for Kernel Error
Posted by alistair23@gmail.com 1 month, 2 weeks ago
From: Alistair Francis <alistair.francis@wdc.com>

Implement From<FromBytesWithNulError> for the Kernel Error type

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
---
 rust/kernel/error.rs | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
index 258b12afdcba..569d9d032ab3 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;
@@ -251,6 +252,12 @@ fn from(e: core::convert::Infallible) -> Error {
     }
 }
 
+impl From<FromBytesWithNulError> for Error {
+    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.
-- 
2.52.0
Re: [RFC v3 07/27] rust: error: impl From<FromBytesWithNulError> for Kernel Error
Posted by Gary Guo 1 month, 1 week ago
On 2026-02-11 03:29, alistair23@gmail.com wrote:
> From: Alistair Francis <alistair.francis@wdc.com>
> 
> Implement From<FromBytesWithNulError> for the Kernel Error type
> 
> Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
> ---
>  rust/kernel/error.rs | 7 +++++++
>  1 file changed, 7 insertions(+)
> 
> diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
> index 258b12afdcba..569d9d032ab3 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;
> @@ -251,6 +252,12 @@ fn from(e: core::convert::Infallible) -> Error {
>      }
>  }
> 
> +impl From<FromBytesWithNulError> for Error {
> +    fn from(_: FromBytesWithNulError) -> Error {
> +        code::EINVAL
> +    }
> +}

Are we sure that `FromBytesWithNulError` maps cleanly to the `EINVAL` 
error code?

Anyhow, please add `#[inline]` for such simple functions.

Best,
Gary

> +
>  /// A [`Result`] with an [`Error`] error type.
>  ///
>  /// To be used as the return type for functions that may fail.
Re: [RFC v3 07/27] rust: error: impl From<FromBytesWithNulError> for Kernel Error
Posted by Alistair Francis 2 weeks, 6 days ago
On Fri, Feb 20, 2026 at 12:49 AM Gary Guo <gary@garyguo.net> wrote:
>
> On 2026-02-11 03:29, alistair23@gmail.com wrote:
> > From: Alistair Francis <alistair.francis@wdc.com>
> >
> > Implement From<FromBytesWithNulError> for the Kernel Error type
> >
> > Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
> > ---
> >  rust/kernel/error.rs | 7 +++++++
> >  1 file changed, 7 insertions(+)
> >
> > diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
> > index 258b12afdcba..569d9d032ab3 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;
> > @@ -251,6 +252,12 @@ fn from(e: core::convert::Infallible) -> Error {
> >      }
> >  }
> >
> > +impl From<FromBytesWithNulError> for Error {
> > +    fn from(_: FromBytesWithNulError) -> Error {
> > +        code::EINVAL
> > +    }
> > +}
>
> Are we sure that `FromBytesWithNulError` maps cleanly to the `EINVAL`
> error code?

`FromBytesWithNulError` means "An error indicating that a nul byte was
not in the expected position." [1]

To me that maps with `EINVAL`

1: https://doc.rust-lang.org/std/ffi/enum.FromBytesWithNulError.html

>
> Anyhow, please add `#[inline]` for such simple functions.

I can, just noting that none of the others have `#[inline]`

Alistair

>
> Best,
> Gary
>
> > +
> >  /// A [`Result`] with an [`Error`] error type.
> >  ///
> >  /// To be used as the return type for functions that may fail.
Re: [RFC v3 07/27] rust: error: impl From<FromBytesWithNulError> for Kernel Error
Posted by Alice Ryhl 2 weeks, 6 days ago
On Fri, Mar 13, 2026 at 3:20 AM Alistair Francis <alistair23@gmail.com> wrote:
>
> On Fri, Feb 20, 2026 at 12:49 AM Gary Guo <gary@garyguo.net> wrote:
> >
> > On 2026-02-11 03:29, alistair23@gmail.com wrote:
> > > From: Alistair Francis <alistair.francis@wdc.com>
> > >
> > > Implement From<FromBytesWithNulError> for the Kernel Error type
> > >
> > > Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
> > > ---
> > >  rust/kernel/error.rs | 7 +++++++
> > >  1 file changed, 7 insertions(+)
> > >
> > > diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
> > > index 258b12afdcba..569d9d032ab3 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;
> > > @@ -251,6 +252,12 @@ fn from(e: core::convert::Infallible) -> Error {
> > >      }
> > >  }
> > >
> > > +impl From<FromBytesWithNulError> for Error {
> > > +    fn from(_: FromBytesWithNulError) -> Error {
> > > +        code::EINVAL
> > > +    }
> > > +}
> >
> > Are we sure that `FromBytesWithNulError` maps cleanly to the `EINVAL`
> > error code?
>
> `FromBytesWithNulError` means "An error indicating that a nul byte was
> not in the expected position." [1]
>
> To me that maps with `EINVAL`
>
> 1: https://doc.rust-lang.org/std/ffi/enum.FromBytesWithNulError.html
>
> >
> > Anyhow, please add `#[inline]` for such simple functions.
>
> I can, just noting that none of the others have `#[inline]`

I think add #[inline] to these could be a nice easy issue for new
contributors. If you want, you can open an issue on our github.
Alice