rust: error: implement Display for Error
PATCH v3 1/1 rust: error: implement Display for Error
Implement the fmt::Display trait for the kernel Error type.
While the existing Debug implementation already outputs the error name (e.g., 'EPERM'), implementing Display allows errors to be formatted using the standard '{}' display marker. This provides a more idiomatic Rust API and allows Error to be used seamlessly with macros that expect the Display trait.
---
Changes in v3:
- Fixed doctest to prevent panics when CONFIG_SYMBOLIC_ERRNAME is disabled.
- Corrected commit message regarding the output of the existing Debug implementation.
- Retained write! for the fallback error case. Applying f.pad() to dynamically generated text requires memory allocation, which is unsafe in error formatting paths.
rust/kernel/error.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
rust/kernel/error.rs +22 -0
@@ -216,6 +216,28 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } } +impl fmt::Display for Error { +/// Displays the error name if available, otherwise the numeric error code. +/// +/// # Examples +/// +/// ``` +/// # use kernel::prelude::*; +/// let err = EPERM; +/// // Formats to "EPERM" or "Unknown error 1" depending on CONFIG_SYMBOLIC_ERRNAME. +/// let _ = fmt!("{err}"); +/// ``` +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { +match self.name () { +// Print out the numeric error code if no name can be found. +None =>write! (f,"Unknown error {}" , -self.0 .get ()), +// SAFETY: These strings are ASCII-only. +Some (name) => f.pad (unsafe { + core::str::from_utf8_unchecked (name.to_bytes ()) + }), + } + } +}impl From <AllocError >for Error {#[inline] fn from (_:AllocError ) ->Error {
Signature · 1 line
2.43.0
Onur Özkan
reviewerHi Arnav,
Please avoid sending multiple versions of the same patch one after another. If you feel your patch isn't good enough, you can reply to your own submission with notes and incorporate those changes along with any feedback from the maintainers. It's unpleasant to see 3 versions of the same patch sent within a short time window.
Also, I suggest to add more people to the cc list, likely all the people listed by the ./scripts/get_maintainer.pl script.
Quoted 5 lines · On Sun, 03 May 2026 22:44:32 +0530 Arnav Sharma <[email protected]> wrote:
Implement the
fmt::Displaytrait for the kernel Error type.While the existing Debug implementation already outputs the error name (e.g., 'EPERM'), implementing Display allows errors to be formatted using the standard '{}' display marker. This provides a more idiomatic Rust API
Can you say more about what you mean with "more idiomatic"? It sounds like a highly subjective opinion.
Regards,
Onur
Quoted 50 lines
and allows Error to be used seamlessly with macros that expect the Display trait.
Signed-off-by Arnav Sharma <[email protected]>---
Changes in v3:
- Fixed doctest to prevent panics whenCONFIG_SYMBOLIC_ERRNAMEis disabled.
- Corrected commit message regarding the output of the existing Debug implementation.
- Retainedwrite!for the fallback error case. Applyingf.pad()to dynamically generated text requires memory allocation, which is unsafe in error formatting paths.rust/kernel/error.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+)rust/kernel/error.rs +22
-0@@ -216,6 +216,28 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { } } +impl fmt::Display for Error { +/// Displays the error name if available, otherwise the numeric error code. +/// +/// # Examples +/// +/// ``` +/// # use kernel::prelude::*; +/// let err = EPERM; +/// // Formats to "EPERM" or "Unknown error 1" depending on CONFIG_SYMBOLIC_ERRNAME. +/// let _ = fmt!("{err}"); +/// ``` +fn fmt (&self, f: &mut fmt::Formatter <'_>) -> fmt::Result { +match self.name () { +// Print out the numeric error code if no name can be found. +None =>write! (f,"Unknown error {}" , -self.0 .get ()), +// SAFETY: These strings are ASCII-only. +Some (name) => f.pad (unsafe { + core::str::from_utf8_unchecked (name.to_bytes ()) + }), + } + } +}impl From <AllocError >for Error {#[inline] fn from (_:AllocError ) ->Error {Signature · 1 line
2.43.0