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

Comments

Projects
None yet
10 participants
@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

This comment has been minimized.

Copy link
Contributor

withoutboats commented Nov 14, 2015

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

This comment has been minimized.

Copy link
Member

jonas-schievink commented Nov 14, 2015

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

@mitchmindtree

This comment has been minimized.

Copy link

mitchmindtree commented Jul 6, 2016

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
@ubsan

This comment has been minimized.

Copy link
Contributor

ubsan commented Jul 6, 2016

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

@nrc nrc added T-lang T-libs labels Aug 19, 2016

@J-F-Liu

This comment has been minimized.

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

This comment has been minimized.

Copy link

russellmcc commented Apr 20, 2017

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

scottmcm commented Apr 11, 2018

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

@scottmcm scottmcm closed this Apr 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.