clone_from

Crates.ioclone_from
lib.rsclone_from
version0.1.0
sourcesrc
created_at2023-03-13 15:30:29.964011
updated_at2023-03-13 15:30:29.964011
descriptionDerive Clone including clone_from
homepage
repositoryhttps://github.com/antiguru/clone_from
max_upload_size
id808997
size19,481
Moritz Hoffmann (antiguru)

documentation

README

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.

Commit count: 2

cargo fmt