Skip to content

Unboxed closures with all Clone members should implement Clone #23501

Closed

Description

@seanmonstar

Similar to #19128

#![feature(core, unboxed_closures)]
use std::ops::Fn;

#[derive(Clone)]
struct NotCopy;

#[derive(Clone)]
struct Derp {
    foo: NotCopy
}

impl Fn<()> for Derp {
    type Output = ();
    extern "rust-call" fn call(&self, _: ()) {
    }
}

fn with_clone<F>(_f: F) where F: Fn() + Clone {

}

fn main() {
    let foo = NotCopy;
    with_clone(Derp { foo: NotCopy }); // works
    with_clone(move || { // errors
        let _ = foo; // <- should be like Derp, accessing self.foo
    });
}

Activity

  1. bluss commented on Apr 27, 2015

    @bluss
    Contributor

    This is important for iterators -- with this we can clone most iterator pipelines. In fact, most don't even need captures at all and would be the cheapest to clone.

  2. withoutboats commented on Nov 14, 2015

    @withoutboats
    Contributor

    This would be good for situations in which one wants to box and store a closure in a small minority of cases. Would be good to be able to have something like Cow<'a, Fn(...)>.

  3. arielb1 commented on Nov 14, 2015

    @arielb1
    Contributor

    Sounds like an nice idea. Unfortunately it would require Clone to be a lang-item, but it is already half a lang-item as a supertrait to Copy.

  4. seanmonstar commented on Nov 14, 2015

    @seanmonstar
    ContributorAuthor

    The expanded closure struct couldn't have the equivalent of 'derive(Clone)'
    added to it?

    On Sat, Nov 14, 2015, 9:32 AM arielb1 [email protected] wrote:

    Sounds like an nice idea. Unfortunately it would require Clone to be a
    lang-item, but it is already half a lang-item as a supertrait to Copy.

    —
    Reply to this email directly or view it on GitHub
    #23501 (comment).

  5. arielb1 commented on Nov 14, 2015

    @arielb1
    Contributor

    @seanmonstar

    derive(Clone) runs before upvars are known.

  6. added
    T-lang
    P-low
    A-closures
    and removed
    A-closures
    on May 4, 2017
  7. added
    C-bug
    C-feature-request
    and removed
    C-bug
    on Jul 22, 2017
  8. oli-obk commented on Nov 24, 2017

    @oli-obk
    Contributor

    I think this can be closed now that we have #44490

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresC-feature-requestP-lowT-lang

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions