# Implementing Clone for generators/async blocks

**URL:** <https://internals.rust-lang.org/t/implementing-clone-for-generators-async-blocks/16233>\
**Category:** compiler\
**Created:** [March 2, 2022, 1:27am UTC](https://internals.rust-lang.org/t/implementing-clone-for-generators-async-blocks/16233 "2022-03-02T01:27:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![canndrew](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/canndrew/32/1676_2.png) [@canndrew](https://internals.rust-lang.org/u/canndrew)\
**Post date:** [March 2, 2022, 1:27am UTC](https://internals.rust-lang.org/t/implementing-clone-for-generators-async-blocks/16233/1 "2022-03-02T01:27:38Z")

</div>

Cross-posting from [the URLO thread](https://users.rust-lang.org/t/cloning-a-generator-async-block/72431). I'm in a situation where I'd really like to be able to clone a generator or async block. Currently, it seems this isn't possible, even if the type could trivially implement clone:

```rust
let wow0 = async {};
let wow1 = Clone::clone(&wow0); // ERROR

```

Is there any obscure way to do this? ie. does the code for implementing `Clone` for these types exist in the compiler in any shape or form but just isn't available to users without some hackery? Failing that, can we make it so that this is supported? Is this something that would require an RFC or would a PR which implements it behind a feature flag be enough?

As far as I can tell, it should be possible for any generator to implement `Clone` so long as (a) all its captured variables implement `Clone`, (b) all its local variables which are held across a `yield`/`await` implement `Clone` and (c) it never holds a self-reference across a `yield`/`await`. Are there any other limitations I'm not seeing?

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cuviper/32/1897_2.png) [@cuviper](https://internals.rust-lang.org/u/cuviper)\
**Post date:** [March 2, 2022, 1:42am UTC](https://internals.rust-lang.org/t/implementing-clone-for-generators-async-blocks/16233/2 "2022-03-02T01:42:51Z")

</div>

I found this issue:

> <https://github.com/rust-lang/rust/issues/57972>
>
> There was \[some discussion on Discord\](https://discordapp.com/channels/442252698…964721669/443150878111694848/539561017722535937) about whether generators can be cloned. It seems to me that non-self-referential generators can be trivially cloneable if everything in their environment implements clone (including both upvars and saved state, the implementation would be non-trivial as it would have to check the current state to know which fields need cloning, but that's basically like the generated \`Drop\` implementation).
> 
> I don't see any way that self-referential generators could be cloneable, if there were some sort of \`fn clone\_into(&self, place: Pin\<\*mut Self\>)\` operation they could potentially support that via re-writing the self-references, but that doesn't exist yet as far as I know. You can clone them before starting by using something like \`impl (FnOnce() -\> impl Generator) + Clone\` instead of taking the generator directly.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [May 31, 2022, 1:43am UTC](https://internals.rust-lang.org/t/implementing-clone-for-generators-async-blocks/16233/3 "2022-05-31T01:43:39Z")

</div>

Closed on May 31, 2022, 1:43am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
