derive-where icon indicating copy to clipboard operation
derive-where copied to clipboard

Feature: Support `skip` on `Clone` derives

Open overdrivenpotato opened this issue 10 months ago • 3 comments

Sometimes, it's useful to derive Clone for types with fields that cannot be cloned, and need to be re-initialized:

#[derive_where(Clone, Debug)]
struct Foo {
    // Defaults to `Option::<TcpStream>::default()` on clone
    #[derive_where(skip(Clone))]
    connection: Option<TcpStream>,
    s: String,
    #[derive_where(skip(Debug))]
    i: i32,
}

Alternatively maybe #[derive_where(default(Clone))] makes more sense here.

overdrivenpotato avatar Jan 21 '25 04:01 overdrivenpotato

I guess using default for something like this wouldn't be too uncommon, looking at std functionality like mem::take.

ModProg avatar Jan 21 '25 09:01 ModProg

serde's derive macro also has a similar feature #[serde(default)]

overdrivenpotato avatar Jan 22 '25 04:01 overdrivenpotato

@daxpedda should this be part of a general skip (would be a breaking change) and it would pose a strange problem in combination with Copy.

Should this be allowed if the type also derives Copy? I guess we need to prevent this if we don't want scenarios where a copy and a clone are !=.

ModProg avatar Jan 30 '25 20:01 ModProg