public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Onur Özkan" <work@onurozkan.dev>
To: Arnav Sharma <arnav4324@gmail.com>
Cc: ojeda@kernel.org, rust-for-linux@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] rust: error: implement Display for Error
Date: Sun,  3 May 2026 21:19:08 +0300	[thread overview]
Message-ID: <20260503181911.13142-1-work@onurozkan.dev> (raw)
In-Reply-To: <20260503171432.7846-1-arnav4324@gmail.com>

Hi 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.

On Sun, 03 May 2026 22:44:32 +0530
Arnav Sharma <arnav4324@gmail.com> wrote:

> 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

Can you say more about what you mean with "more idiomatic"? It sounds like
a highly subjective opinion.

Regards,
Onur

> and allows Error to be used seamlessly with macros that expect the
> Display trait.
> 
> Signed-off-by: Arnav Sharma <arnav4324@gmail.com>
> ---
> 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(+)
> 
> diff --git a/rust/kernel/error.rs b/rust/kernel/error.rs
> index 05cf869ac090..4ab7d7d8ccad 100644
> --- a/rust/kernel/error.rs
> +++ b/rust/kernel/error.rs
> @@ -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 {
> -- 
> 2.43.0
> 

      reply	other threads:[~2026-05-03 18:19 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-03 17:14 [PATCH v3] rust: error: implement Display for Error Arnav Sharma
2026-05-03 18:19 ` Onur Özkan [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260503181911.13142-1-work@onurozkan.dev \
    --to=work@onurozkan.dev \
    --cc=arnav4324@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox