Automatic clones in async code

⚓ Rust    📅 2025-12-15    👤 surdeus    👁️ 1      

surdeus

Hi,

I have a feelthing that the answere will be "No, its not possible", but since its turning out to be quite a issue for the readability of code I wanted to double check that there is no pattern I have missed.

I.n async code (which tends to polute quite a bit, so ends up in a larger part of the codebase when parts of it will be served from a http server) I end up with quite a few structs that are Arc-ified, I.e stuff like

Arc<FunctionalityProvider1>
Arc<FunctionalityProvider2>
etc.

The "issue" is that they are Clone only, not copy, so whenever I want to spawn tasks / async context to operations on them I end up with lots (and lots) of .clone() all over the codebase.

Here I think it "hides" that some clones might actually be expensive, i.e. the code becomes so full of .clone() on Arc that "realy are not that expensive" so that one stop to reacts to seeing .clone().

I really do like that Rust make clones explicit in general - helps me avoid unneeded work - but in async code they are all over the place.

Does anyone have any good pattern to "avoid" .clone()'s like for example:

{
   let a = a.clone();
   let b = b.clone();
   let c = c.clone();
   let d = d.clone();
   spawn(async move { /* use them */});


}

7 posts - 4 participants

Read full topic

🏷️ Rust_feed