Re: [PATCH] rust/revocable: add try_with() convenience method
From: Benno Lossin
Date: Sat Mar 15 2025 - 13:48:19 EST
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.
---
Cheers,
Benno