Module :: clone_dyn
Derive to clone dyn structures.
This crate is a facade that re-exports clone_dyn_types (for core traits and logic) and clone_dyn_meta (for procedural macros). It provides a convenient way to enable cloning for trait objects. By default, Rust does not support cloning for trait objects due to the Clone trait requiring compile-time knowledge of the type's size. The clone_dyn crate addresses this limitation through its procedural macros, allowing for cloning collections of trait objects. The crate's purpose is straightforward: it allows for easy cloning of dyn< Trait > with minimal effort and complexity, accomplished by applying the #[clone_dyn] attribute to the trait.
Alternative
There are few alternatives dyn-clone, dyn-clonable. Unlike other options, this solution is more concise and demands less effort to use, all without compromising the quality of the outcome.
Basic use-case
This example demonstrates the usage of the #[clone_dyn] attribute macro to enable cloning for trait objects.
// Use fully qualified path
When using clone_dyn with multithreaded or asynchronous code, the #[clone_dyn] macro automatically generates Clone implementations for all four trait object variants:
Box<dyn Trait>Box<dyn Trait + Send>Box<dyn Trait + Sync>Box<dyn Trait + Send + Sync>
For a complete working example demonstrating this pattern with iterators, see examples/clone_dyn_trivial.rs.
Try out cargo run --example clone_dyn_trivial.
See code.
To add to your project
Try out from the repository