yewstack / implicit-clone

Immutable types and ImplicitClone trait similar to Copy
19 stars 10 forks source link

`ImplicitClone` for `Copy` types #32

Open kirillsemyonkin opened 1 year ago

kirillsemyonkin commented 1 year ago

Issue for implementing ImplicitClone for all Copy types.

As noted in https://github.com/yewstack/yew/discussions/3464#discussioncomment-7275776, the entirety of all Copy types may receive an ImplicitClone implementation via specializations (currently nightly):

#![feature(specialization)]

default impl<T: Copy> ImplicitClone for T {
}

However, the RFC for specializations is nowhere near being done, so for this point in time ImplicitClone will still stay limited in the amount of types that will be implemented (and this issue will stay open).

Alternatively, a Rust RFC should be made that would allow ImplicitClone for Copy types: the only blocking suggestion from the Rust compiler is that the trait may have colliding implementations in future, which is not correct since the marker trait is empty, and there is nothing to collide with. The RFC would allow repeating marker trait implementations. Comment if someone makes such an RFC and I will update this issue.

cecton commented 1 year ago

I love this alternative idea actually. I understand specialization is complicated and requires a lot of time to stabilize but the concept of ImplicitClone is actually very simple and could already be implemented somehow.

Maybe a wild idea but why not even make an RFC to actually move ImplicitClone to the standard Rust compiler? Copy in its current state holds 2 different concepts: the memory being copied (memcpy) instead of cloning (trait Clone) + implicit copy instead of moving ownership. This could be made into two traits (Copy and ImplicitClone) so types like Rc and Arc can be moved easily into closures. It would be so much more user-friendly.

kirillsemyonkin commented 1 year ago

@cecton

kirillsemyonkin commented 1 year ago

Marker traits may indeed have implementation collisions:

cecton commented 1 year ago

That one? https://crates.io/crates/marker_trait

kirillsemyonkin commented 1 year ago

That one? https://crates.io/crates/marker_trait

Not sure what's that tiny crate you linked does. A marker trait is a trait that has no required methods.