Skip to content

Cloneable generators #57972

Closed
Closed
@Nemo157

Description

@Nemo157

There was some discussion on Discord 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-coroutinesArea: CoroutinesC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-langRelevant to the language team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions