Re: [PATCH] rust/revocable: add try_with() convenience method

From: Danilo Krummrich
Date: Sun Mar 16 2025 - 08:42:56 EST


On Sun, Mar 16, 2025 at 09:20:03PM +0900, Alexandre Courbot wrote:
> On Sun Mar 16, 2025 at 2:48 AM JST, Benno Lossin wrote:
> > On Sat Mar 15, 2025 at 3:26 PM CET, Alexandre Courbot wrote:
> >> 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:
> >>>> 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.
> >
> > You could have the following signature:
> >
> > fn try_access_with<R>(&self, on_vacant: Error, f: impl FnOnce(&T) -> Result<R>) -> Result<R>;
> >
> > That will use the `on_vacant` error instead of hard coding ENXIO. But
> > maybe it's better to just have such a wrapper in drivers that actually
> > need it (ie even with the concrete error specified and not a parameter).
> > You'll know better through actually trying to write a driver.
>
> Yeah, having the extra on_vacant parameter would require callers to
> specify the error they need every time, where it is supposed to be
> a per-driver constant.

I think the signature proposed by Benno is useful, since even specific drivers
may return a different error code depending on the semantic context of where
this call fails.

> So I guess a per-driver macro or wrapper would be more ergonomic in the end.

I think this makes sense as an addition on top of the above.