public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: Gary Guo <gary@garyguo.net>
To: Joel Fernandes <joelagnelf@nvidia.com>
Cc: John Hubbard <jhubbard@nvidia.com>, Timur Tabi <ttabi@nvidia.com>,
	"dakr@kernel.org" <dakr@kernel.org>,
	Alexandre Courbot <acourbot@nvidia.com>,
	"lossin@kernel.org" <lossin@kernel.org>,
	"a.hindborg@kernel.org" <a.hindborg@kernel.org>,
	"boqun.feng@gmail.com" <boqun.feng@gmail.com>,
	"aliceryhl@google.com" <aliceryhl@google.com>,
	Zhi Wang <zhiw@nvidia.com>, "simona@ffwll.ch" <simona@ffwll.ch>,
	"alex.gaynor@gmail.com" <alex.gaynor@gmail.com>,
	"ojeda@kernel.org" <ojeda@kernel.org>,
	"tmgross@umich.edu" <tmgross@umich.edu>,
	"nouveau@lists.freedesktop.org" <nouveau@lists.freedesktop.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"rust-for-linux@vger.kernel.org" <rust-for-linux@vger.kernel.org>,
	"bjorn3_gh@protonmail.com" <bjorn3_gh@protonmail.com>,
	Edwin Peer <epeer@nvidia.com>,
	"airlied@gmail.com" <airlied@gmail.com>,
	"bhelgaas@google.com" <bhelgaas@google.com>,
	Alistair Popple <apopple@nvidia.com>
Subject: Re: [PATCH] gpu: nova-core: bitfield: use &mut self setters instead of builder pattern
Date: Thu, 1 Jan 2026 04:28:51 +0000	[thread overview]
Message-ID: <20260101042851.08576bdb.gary@garyguo.net> (raw)
In-Reply-To: <4482eb34-0961-4b40-868f-77dc001d98e8@nvidia.com>

On Wed, 31 Dec 2025 23:05:21 -0500
Joel Fernandes <joelagnelf@nvidia.com> wrote:

> Hi John,
> 
> On 12/31/2025 9:52 PM, John Hubbard wrote:
> [..]
> >>> But this really is fake chaining, because there are no Results involved.
> >>> It's not buying us anything except a bit of indirection and cognitive
> >>> load for the reader.  
> >>
> >> Chaining is not really only about error propagation. Builder pattern can be used
> >> for other cases too, like passing a setter chained expression to a function
> >> argument for better clarity, I was planning to do that for the sequencer for
> >> instance since there's a lot of parameters passed to it.  
> > 
> > Let's see if that has any use for this.
> > 
> > So far, though, in the code base that we have today, there is absolutely
> > zero benefit. The diffs here prove it.
> >   
> 
> From your patch diff, I see the lines of code increased. But that's not even the
> main issue I have with it (though IMO the chaining is more readable..).
> 
> >> But in this case, I am actually absolutely opposed against this, it makes the
> >> API hard to use because now how do you differentiate between an IO function call
> >> versus something that just mutates memory? Is set() an IO or write()?  
> > 
> > That's a completely separate, pre-existing issue with the API.  
> 
> Nope. With chaining we clearly know that the final operation is a write().
> 
> For instance, you cannot do:
>   reg.set_foo()
>      .write()
>      .set_bar()
> 
> That wont compile. You cannot intermingle write() with set_XX() because write()
> doesn't return anything that can be chained with. The builder pattern is typically:
>   obj.set_something()
>      .set_something()
>      .do_some_action()
> 
> The 'set' can also be 'with' from what I've seen, whatever. The point is the
> last thing is the action. IMO very readable and simple. I know that the write()
> will be what ends up doing the I/O. It is one entity that culminates in the write().
> 
> >   
> >>
> >>       reg.set_foo(x);   // no IO
> >>       reg.set_sec(y);
> >>       reg.write(bar);   // IO.
> >>
> >> So no thank you, I quite dislike it. :)
> >>
> >> Instead with chaining, we can just rely on the last part of the chain concluding
> >> in a write() with the intermediaries just mutating memory.  
> > 
> > Same as above, just a more happy-happy chaining interface, but the same
> > function calls must be made in the same order.  
> 
> No, you cannot place write() anywhere except at the end of the chain - the type
> system enforces this since write() returns ().

One thing that probably should be added though is `#[must_use]`
annotations on these set functions; this would ensure that if someone
writes

	reg.set_foo();

the compiler would complain that the return value is not used.

Best,
Gary



  reply	other threads:[~2026-01-01  4:29 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-12-31 21:47 [PATCH] gpu: nova-core: bitfield: use &mut self setters instead of builder pattern John Hubbard
2025-12-31 22:33 ` Timur Tabi
2025-12-31 22:47   ` John Hubbard
2026-01-01  0:15     ` Joel Fernandes
2026-01-01  0:46       ` John Hubbard
2026-01-01  2:42         ` Joel Fernandes
2026-01-01  2:52           ` John Hubbard
2026-01-01  4:05             ` Joel Fernandes
2026-01-01  4:28               ` Gary Guo [this message]
2026-01-01 10:01                 ` Joel Fernandes
2026-01-01  8:42 ` kernel test robot

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=20260101042851.08576bdb.gary@garyguo.net \
    --to=gary@garyguo.net \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=airlied@gmail.com \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=apopple@nvidia.com \
    --cc=bhelgaas@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=epeer@nvidia.com \
    --cc=jhubbard@nvidia.com \
    --cc=joelagnelf@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lossin@kernel.org \
    --cc=nouveau@lists.freedesktop.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=simona@ffwll.ch \
    --cc=tmgross@umich.edu \
    --cc=ttabi@nvidia.com \
    --cc=zhiw@nvidia.com \
    /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