-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Clone docs say it can't be derived for function pointers but it can #73480
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
Comments
The wording is incorrect, but the idea is the same: derived implementation will be restricted to Consider this: #![allow(unused)]
#[derive(Clone)]
struct Generate<T>(fn() -> T);
struct NonClone();
fn non_clone() -> NonClone {
NonClone()
}
#[derive(Clone)]
struct CanClone();
fn can_clone() -> CanClone {
CanClone()
}
fn main() {
let g = Generate(non_clone);
// g.clone(); // compile error!
let g = Generate(can_clone);
g.clone();
} Basically, generated implementation looks like this: impl<T> Clone for Generate<T>
where T: Clone
{
fn clone(&self) -> Self {...}
} But you'd want it to be simpler, like in the doc: impl<T> Clone for Generate<T> {
fn clone(&self) -> Self {
*self
}
} And that's exactly what I did myself here: https://github.com/ratijas/qmetaobject-rs/blob/72b349b65dad3135fcc344d2268c3a453e0ac844/qmetaobject/src/connections.rs#L65-L72 Nice profile picture, btw. |
See #26925 for a long debate about that derived behavior. |
Oh, i see what's happening here. There is another weird phrasing in the Copy docs which confused me:
I interpreted "place Copy bounds" as putting an invisible
I thought Derived was equivalent to BoundOnStruct but it's actually like BoundOnImpl. I should have probably known better at this point but at least i am not the only one mislead by it - maybe that should be rephrased too? |
Explain more clearly why `fn() -> T` can't be `#[derive(Clone)]` Closes rust-lang#73480 The derived impls were generated with `rustc -Z unpretty=expanded main.rs` and the raw output is: ```rust struct Generate<T>(fn() -> T); #[automatically_derived] impl<T: ::core::marker::Copy> ::core::marker::Copy for Generate<T> { } #[automatically_derived] impl<T: ::core::clone::Clone> ::core::clone::Clone for Generate<T> { #[inline] fn clone(&self) -> Generate<T> { Generate(::core::clone::Clone::clone(&self.0)) } } ```
The text is here:
However, it's in fact possible to derive:
(playground)
The text was updated successfully, but these errors were encountered: