public inbox for rust-for-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: "Alexandre Courbot" <acourbot@nvidia.com>
To: "Danilo Krummrich" <dakr@kernel.org>
Cc: "Benno Lossin" <benno.lossin@proton.me>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] rust/revocable: add try_with() convenience method
Date: Sat, 15 Mar 2025 23:26:02 +0900	[thread overview]
Message-ID: <D8GWQXK312SX.2RI3EOHM1QGG3@nvidia.com> (raw)
In-Reply-To: <Z9WL-95sJ0DCpaPa@pollux>

On Sat Mar 15, 2025 at 11:17 PM JST, Danilo Krummrich wrote:
> On Sat, Mar 15, 2025 at 11:07:44PM +0900, Alexandre Courbot wrote:
>> On Fri Mar 14, 2025 at 2:50 AM JST, Benno Lossin wrote:
>> > On Thu Mar 13, 2025 at 4:48 PM CET, Danilo Krummrich wrote:
>> >> On Thu, Mar 13, 2025 at 03:38:55PM +0000, Benno Lossin wrote:
>> >>> On Thu Mar 13, 2025 at 4:08 PM CET, Alexandre Courbot wrote:
>> >>> > On Thu Mar 13, 2025 at 11:19 PM JST, Benno Lossin wrote:
>> >>> >> Would it make sense to not use `Result` here and continue with `Option`?
>> >>> >
>> >>> > We would have to return an Option<Result<R>> in this case. The current
>> >>> > code folds the closure's Result into the one of the guard's acquisition
>> >>> > for convenience.
>> >>> >
>> >>> > Actually, I don't think I have ever used try_access() a single time
>> >>> > without converting its returned Option into a Result. Wouldn't it make
>> >>> > sense to do the opposite, i.e. make try_access() return Err(ENXIO) when
>> >>> > the guard cannot be acquired and document this behavior?
>> >>> 
>> >>> Sure, if you're always doing
>> >>> 
>> >>>     let guard = rev.try_access().ok_or(ENXIO)?;
>> >>> 
>> >>> Then it makes sense from my view, maybe Danilo has some other argument
>> >>> for why `Option` is better.
>> >>
>> >> Most of the time I think we indeed want to derive an Err() if try_access()
>> >> fails, but not with a specific error code. The error code depends on the context
>> >> of where the revocable is used (e.g. for I/O mappings), but it also depends on
>> >> the driver semantics.
>> >
>> > In that case a single function with this signature would make sense:
>> >
>> >     fn access_with<R>(&self, f: impl FnOnce(&T) -> R) -> Option<R>;
>> >
>> > If there are common usages that always return the same error code, then
>> > we could add them as functions with `Result`.
>> 
>> Yeah the more I think about it the more this seems to make sense,
>> from a strictly logical point of view.
>> 
>> Where I am still on the fence is that the goal is also to reduce the
>> friction introduced by the Revocable business, which a large driver
>> might need to interact with hundreds of times. If the user wants the
>> callback to return a Result, then this method will return an
>> Option<Result>. One would then need to ok_or the Option, then flatten
>> the two results, which is a bit verbose.
>
> I think you see this from the perspective of one specific usecase, i.e.
> Devres<T>, where T dereferences to Io, right?

Indeed.

>
>> I suppose drivers could add their own macros to do that automatically
>> and reduce code verbosity, at the cost of less cohesion across drivers.
>> Guess I'll go with that if I cannot come with anything better.
>
> Maybe we could do something more specific but yet generic on top (for the
> use-case above), but we still can't assume the exact error code a driver wants
> to derive from failing try_access(). So, maybe a driver specific wrapper is
> indeed what you want on top of what this patch provides.

So be it! It's not that bad and more flexible in the end.

  reply	other threads:[~2025-03-15 14:26 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-13 12:40 [PATCH] rust/revocable: add try_with() convenience method Alexandre Courbot
2025-03-13 14:19 ` Benno Lossin
2025-03-13 15:08   ` Alexandre Courbot
2025-03-13 15:38     ` Benno Lossin
2025-03-13 15:48       ` Danilo Krummrich
2025-03-13 17:50         ` Benno Lossin
2025-03-15 14:07           ` Alexandre Courbot
2025-03-15 14:17             ` Danilo Krummrich
2025-03-15 14:26               ` Alexandre Courbot [this message]
2025-03-15 17:48                 ` Benno Lossin
2025-03-16 12:20                   ` Alexandre Courbot
2025-03-16 12:42                     ` Danilo Krummrich
2025-03-13 14:37 ` Danilo Krummrich
2025-03-13 15:11   ` Alexandre Courbot

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=D8GWQXK312SX.2RI3EOHM1QGG3@nvidia.com \
    --to=acourbot@nvidia.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=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --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