Skip to content
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

Make Clone a lang-item and implement it for closures whose members are all Clone #1369

Closed
arielb1 opened this issue Nov 14, 2015 · 8 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.

Comments

@arielb1
Copy link
Contributor

arielb1 commented Nov 14, 2015

moved from rust-lang/rust#23501

@arielb1 arielb1 changed the title Make Clone a lang-item Make Clone a lang-item and implement it for closures whose members are all Clone Nov 14, 2015
@withoutboats
Copy link
Contributor

Could this be implemented using default impls of Clone and Copy? I know that would be a pretty serious breaking change, because there could be types that are only sound because they don't implement Clone. But in principle, wouldn't closures be Clone if their environment is Clone if that were the case (or do default impls not apply to closures)?

@jonas-schievink
Copy link
Contributor

This is also needed to fix rust-lang/rust#28229

@mitchmindtree
Copy link

Just thought I'd 👍 this and mention a couple use-cases I come across quite frequently:

  • Iterator ergonomics - being able to clone Iterators with some F: Fn + Clone field
  • real-time thread synchronisation - being able to clone a F: Fn(&mut State) + Clone and send it to multiple real-time threads across via channels without requiring Arc

@strega-nil
Copy link

Not just closures. Functions and arrays would be great as well.

@nrc nrc added T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC. labels Aug 19, 2016
@J-F-Liu
Copy link

J-F-Liu commented Jan 22, 2017

Meet this same issue, here is an example:

#[derive(Debug)]
struct Take(String);

fn make<F>(func: F) -> Box<Fn(&str) -> Take>
    where F: FnOnce(String) -> Take + Copy + 'static
{
     Box::new(move |s| {
         func(s.to_string())
     })
}

fn main() {
    let m = make(|s|Take(s));
    println!("{:?}", m("abc"));
}

should compile.

@russellmcc
Copy link

I've started hitting this a lot, mostly in the "iterator ergonomics" that @mitchmindtree mentioned. I'm not sure why Clone needs to be a lang item (maybe there's something I'm missing), but having appropriate closures implement Clone sure would be convenient.

@cramertj
Copy link
Member

cramertj commented Aug 28, 2017

Since Clone was made a lang item in rust-lang/rust#43690, I've written this up as #2132.

@scottmcm
Copy link
Member

Since the RFC was accepted, closing in favour of the tracking issue rust-lang/rust#44490

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC. T-libs-api Relevant to the library API team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

10 participants