Skip to content

Cloneable generators #57972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Nemo157 opened this issue Jan 29, 2019 · 4 comments
Closed

Cloneable generators #57972

Nemo157 opened this issue Jan 29, 2019 · 4 comments
Labels
A-coroutines Area: Coroutines C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@Nemo157
Copy link
Member

Nemo157 commented Jan 29, 2019

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.

@jonas-schievink jonas-schievink added C-feature-request Category: A feature request, i.e: not implemented / a PR. A-coroutines Area: Coroutines labels Jan 29, 2019
@tema3210
Copy link

tema3210 commented May 6, 2019

The Clone trait, like Move, uses memcpy() function to copy it's data to another location. The simplest way i can propose is to have a OnMove trait to use in object lifecycle. Since the arichmetic isn't so demandful to hardware resources, this solution sounds better to me. Moving from one pin to another sounds good, especially for the gurantee that generator won't be moved too frequently. The most impotant question is syntax: how can we shortly mark self-refs?. The implementation i can propose is:
``
trait OnMove {
unsafe fn Update(*mut self,u64 delta){
//there for each reference should be adding the delta.
}
unsafe fn GetDelta(*mut self,*mut to){
to-self;//there value only adresses.
}

}
``

@jonas-schievink jonas-schievink added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Jan 23, 2020
@jonas-schievink
Copy link
Contributor

I think I can say fairly confidently that OnMove is not going to happen. It has been discussed and rejected many times in the past already.

It looks like supporting this for self-referential generators would run into #63818 too, so limiting it to movable generators seems like the only option. A simplification would be to first limit it to Copyable generators, which would keep things fairly simple since no decisions based on the generator state have to be made.

@bouk
Copy link
Contributor

bouk commented Oct 19, 2022

Related: #95360

@Nemo157
Copy link
Member Author

Nemo157 commented Oct 19, 2022

Looks like #95360 fully covers this.

@Nemo157 Nemo157 closed this as completed Oct 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-coroutines Area: Coroutines C-feature-request Category: A feature request, i.e: not implemented / a PR. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants