Skip to content

Function type does not implement Clone #15444

New issue

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

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

Already on GitHub? # to your account

Closed
am0d opened this issue Jul 5, 2014 · 7 comments · Fixed by #19780
Closed

Function type does not implement Clone #15444

am0d opened this issue Jul 5, 2014 · 7 comments · Fixed by #19780
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@am0d
Copy link
Contributor

am0d commented Jul 5, 2014

I'm having some troubles with Clone for a function type.
I'm using the following function in my code:

fn(&Request, &mut ResponseWriter) -> Box<Action>

and it apparently doesn't implement Clone, nor can I implement Clone for it.

This is the code that I'm using to test: http://is.gd/3cm94D
and the output I get when trying to compile code which requires the function to be Cloneable:

<anon>:20:5: 20:35 error: failed to find an implementation of trait core::clone::Clone for fn(&Request, &mut ResponseWriter) -> Box<Action>
<anon>:20     routes: Router<RequestHandler>
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: in expansion of #[deriving]
<anon>:18:1: 19:7 note: expansion site

@Kimundi on irc suggested the following code: http://is.gd/WJCznj which works for this specific test case, but requires a lot of other changes in my code that I haven't been to get working yet.

@huonw
Copy link
Member

huonw commented Jul 5, 2014

I think this is a dupe of #14820. My investigation in #15191 (comment) suggests it should be possible to work around this by providing an explicit Clone impl for Server, rather than deriving it.

@am0d
Copy link
Contributor Author

am0d commented Jul 5, 2014

I haven't been able to find a way to explicitly impl Clone for Server yet that works with this. I'll keep trying a bit, but may just have to wait till the fix is implemented in rustc itself.

@huonw
Copy link
Member

huonw commented Jul 5, 2014

impl Clone for Server {
    fn clone(&self) -> Server {
        let mut handlers = self.routes.handlers.iter().map(|x| *x);
        Server { routes: Router { handlers: handlers.collect() } }
    }
}

@am0d
Copy link
Contributor Author

am0d commented Jul 5, 2014

Thanks Huon. I will play with that a bit and see if I can make it work in my code.

@jjpe
Copy link

jjpe commented Aug 1, 2014

Perhaps not directly related to the posts but it is to the title: how difficult would it be to allow rust programmers to implement user-defined traits for fn types without destroying type safety guarantees?
It doesn't seem to be possible atm but would be a nice feature.

@sfackler
Copy link
Member

sfackler commented Aug 1, 2014

It's possible right now: http://is.gd/kF8HbG.

@jjpe
Copy link

jjpe commented Aug 3, 2014

Thanks @sfackler, that helped quite a bit.

@ghost ghost added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Oct 30, 2014
bors added a commit that referenced this issue Dec 18, 2014
Closes #5988.
Closes #10176.
Closes #10456.
Closes #12744.
Closes #13264.
Closes #13324.
Closes #14182.
Closes #15381.
Closes #15444.
Closes #15480.
Closes #15756.
Closes #16822.
Closes #16966.
Closes #17351.
Closes #17503.
Closes #17545.
Closes #17771.
Closes #17816.
Closes #17897.
Closes #17905.
Closes #18188.
Closes #18232.
Closes #18345.
Closes #18389.
Closes #18400.
Closes #18502.
Closes #18611.
Closes #18783.
Closes #19009.
Closes #19081.
Closes #19098.
Closes #19127.
Closes #19135.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants