1 unstable release

0.1.0 Mar 13, 2023

#1218 in Procedural macros

Download history 105/week @ 2024-07-20 531/week @ 2024-07-27 379/week @ 2024-08-03 353/week @ 2024-08-10 280/week @ 2024-08-17 397/week @ 2024-08-24 428/week @ 2024-08-31 233/week @ 2024-09-07 305/week @ 2024-09-14 415/week @ 2024-09-21 260/week @ 2024-09-28 185/week @ 2024-10-05 182/week @ 2024-10-12 412/week @ 2024-10-19 408/week @ 2024-10-26 387/week @ 2024-11-02

1,472 downloads per month

Apache-2.0 OR MIT

9KB
127 lines

Derive Clone including clone_from

This crate offers a derive macro called CloneFrom to derive Clone with a specialized clone_from implementation.

When deriving Clone, Rust omits a specialized clone_from implementation and defers to clone instead. This means that it can behave counter-intuitively, for example when it should reuse allocations, but does not. This crate aims to be a drop-in replacement for deriving Clone, but with a proper clone_from implementation.

Example

#[derive(CloneFrom)]
struct MyStruct<T> {
    name: String,
    inner: Vec<T>,
}

Considerations

Adding a clone_from implementation to all types in Rust seems to add a significant compile-time cost, which is why Rust does not provide this by default. Many types do not benefit from a specialized clone_from implementation, so it's important to add it when it brings a benefit.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.5MB
~37K SLoC