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

Lifetime-aware Clone #105

Open
alxiong opened this issue Aug 15, 2022 · 1 comment
Open

Lifetime-aware Clone #105

alxiong opened this issue Aug 15, 2022 · 1 comment
Labels
bug The crate does not work as expected

Comments

@alxiong
Copy link

alxiong commented Aug 15, 2022

Describe the bug
Current #[derivative(Clone)] does not work on struct with specific lifetime

To Reproduce

#[macro_use]
extern crate derivative;

#[derive(Derivative)]
#[derivative(Clone)]
struct MyStruct<'a> {
    pub borrowed: &'a String,
}

will give:

  --> src/main.rs:45:10
   |
45 | #[derive(Derivative)]
   |          ^^^^^^^^^^ expected `&String`, found struct `String`
   |
   = note: this error originates in the derive macro `Derivative` (in Nightly builds, run with -Z macro-backtrace for more info)

Expected behavior
Here's what current version of macro expands to:

#[allow(unused_qualifications)]
impl<'a> ::core::clone::Clone for MyStruct<'a> {
    fn clone(&self) -> Self {
        match *self {
            MyStruct {
                borrowed: ref __arg_0,
            } => MyStruct {
                borrowed: (*__arg_0).clone(),
            },
        }
    }
}

what we need:

impl<'a> Clone for MyStruct<'a> {
    fn clone(&self) -> Self {
        Self {
            borrowed: self.borrowed, // instead of the default, which is: `self.borrowed.clone()`
        }
    }
}

Thanks!

@alxiong alxiong added the bug The crate does not work as expected label Aug 15, 2022
@toyboot4e
Copy link

Interesting! Something like <#FieldType as Clone>::clone(#field_ref) needs to be produced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug The crate does not work as expected
Projects
None yet
Development

No branches or pull requests

2 participants