Re: [RFC PATCH] rust: workqueue: Add an example for try_spawn()
From: Benno Lossin
Date: Thu Jul 31 2025 - 05:30:25 EST
On Wed Jul 30, 2025 at 9:38 PM CEST, Boqun Feng wrote:
> On Wed, Jul 30, 2025 at 09:28:05PM +0200, Benno Lossin wrote:
>> On Wed Jul 30, 2025 at 6:34 PM CEST, Boqun Feng wrote:
>> > + /// workqueue::system().try_spawn(
>> > + /// flags::GFP_KERNEL,
>> > + /// {
>> > + /// let work_done = work_done.clone();
>> > + /// let data = data.clone();
>> > + /// move || {
>> > + /// *data.lock() = 42;
>> > + /// work_done.complete_all();
>> > + /// }
>> > + /// }
>> > + /// )?;
>>
>> Not doing your pattern and instead adding a `2` postfix we get:
>>
>> let work_done2 = work_done.clone();
>> let data2 = data.clone();
>>
>
> Yeah, the thing I want to achieve with my pattern is: make it clear that
> the work and the task that queues the work are sharing the same
> `work_done` and `data` (well, no the same `Arc` exactly, but the `Arc`s
> that are pointing to the same object). This pattern here doesn't show
> that clearly imo.
I think it's fine, that pattern is often used for that. Not heavily
opposed to doing it your way, but I feel like the code looks a bit weird
& my instinct is to move the let bindings out (which would produce code
that doesn't compile).
> That said, I'm not really against using `work_done2` and `data2`, just
> I'm afraid that may be more confusing.
I don't think that's a problem.
>> workqueue::system().try_spawn(flags::GFP_KERNEL, move || {
>> *data2.lock() = 42;
>> work_done2.complete_all();
>> })?;
>>
>> There are some discussions of introducing some better syntax for (cheap)
>> cloning, so maybe we can use that in the future.
>
> Do you have links to these discussions.
It's an RFC:
https://github.com/rust-lang/rfcs/pull/368
There probably are more discussions on zulip, but I haven't read those.
The RFC also has a project goal:
https://rust-lang.github.io/rust-project-goals/2025h2/ergonomic-rc.html
---
Cheers,
Benno