rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Boqun Feng <boqun.feng@gmail.com>
To: JCKeep <2407018371@qq.com>
Cc: "Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <benno.lossin@proton.me>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	rust-for-linux@vger.kernel.org
Subject: Re: [PATCH] rust: Implement fmt::Display for kernel Box<T, A>
Date: Mon, 28 Oct 2024 09:43:15 -0700	[thread overview]
Message-ID: <Zx-_IzvBzL1TjQ39@Boquns-Mac-mini.local> (raw)
In-Reply-To: <tencent_CA5A5D6268F0D1419BAFA6AFB4EB5A37920A@qq.com>

Hi,

Thank you for the patch! A few comments below:

First, the patch title should be as tight as possible, please consider
using something like:

	rust: alloc: Implement Display for Box

* `Display` is a common trait in Rust, so can avoid mention its mod name
  (i.e. "fmt::").

* Since this is a *kernel* patch, so the word "kernel" can be avoided.

* The generic parameter is not related to the impl, so can be avoid.

On Mon, Oct 28, 2024 at 10:52:47PM +0800, JCKeep wrote:
> Currently fmt::Display is not implemented for Box<T, A>. We can
> make the following code work by implementing fmt::Display.
> 

Usually phrases like "We can" are not preferred in changelogs,
especially when describing the "why" part of the changes, because in
this part you want to describe the current situation based on *facts*
not on *desires*. So how about:

    Currently `impl Display` is missing for `Box<T, A>`, as a result,
    things like using `Box<..>` directly as an operand in `pr_info!()`
    are impossible, which is less ergonomic compared to `Box` in Rust
    std.

    Therefore add `impl Display` for `Box`.

?

> let v = KBox::new(42, GFP_KERNEL)?;
> pr_info!("{}", v);
> 
> Link: https://github.com/Rust-for-Linux/linux/issues/1126

Please use a Closes tag to refer an issue you want to close on GitHub.

Regards,
Boqun

> Signed-off-by: JCKeep <2407018371@qq.com>
> ---
>  rust/kernel/alloc/kbox.rs | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
> index d69c32496..5f4765800 100644
> --- a/rust/kernel/alloc/kbox.rs
> +++ b/rust/kernel/alloc/kbox.rs
> @@ -427,6 +427,16 @@ fn deref_mut(&mut self) -> &mut T {
>      }
>  }
>  
> +impl<T, A> fmt::Display for Box<T, A>
> +where
> +    T: ?Sized + fmt::Display,
> +    A: Allocator,
> +{
> +    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
> +        fmt::Display::fmt(&**self, f)
> +    }
> +}
> +
>  impl<T, A> fmt::Debug for Box<T, A>
>  where
>      T: ?Sized + fmt::Debug,
> -- 
> 2.34.1
> 

  parent reply	other threads:[~2024-10-28 16:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-28 14:52 [PATCH] rust: Implement fmt::Display for kernel Box<T, A> JCKeep
2024-10-28 15:03 ` Tamir Duberstein
2024-10-28 15:58 ` Miguel Ojeda
2024-10-28 16:43 ` Boqun Feng [this message]
2024-10-28 16:52   ` Miguel Ojeda
2024-10-28 17:39     ` Boqun Feng

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=Zx-_IzvBzL1TjQ39@Boquns-Mac-mini.local \
    --to=boqun.feng@gmail.com \
    --cc=2407018371@qq.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=gary@garyguo.net \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /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;
as well as URLs for NNTP newsgroup(s).