12 breaking releases

0.15.0 Mar 26, 2024
0.13.0 Mar 16, 2024
0.4.0 Oct 18, 2023
0.1.0 Jun 26, 2022

#1058 in Algorithms

Download history 194/week @ 2023-12-22 122/week @ 2023-12-29 190/week @ 2024-01-05 242/week @ 2024-01-12 174/week @ 2024-01-19 175/week @ 2024-01-26 184/week @ 2024-02-02 226/week @ 2024-02-09 419/week @ 2024-02-16 406/week @ 2024-02-23 835/week @ 2024-03-01 1981/week @ 2024-03-08 4464/week @ 2024-03-15 1236/week @ 2024-03-22 733/week @ 2024-03-29 501/week @ 2024-04-05

7,520 downloads per month
Used in 5 crates (via derive_tools)

MIT license

11KB
81 lines

Module :: clone_dyn

experimentalrust-statusdocs.rsOpen in Gitpod discord

Derive to clone dyn structures.

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 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 derive 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. Also, you can ask an inquiry and get answers, which is problematic in the case of alternatives.

Basic use-case

# #[ cfg( all( feature = "enabled", any( not( feature = "no_std" ), feature = "use_alloc" ) ) ) ]
# {
  use clone_dyn::clone_dyn;

  #[ clone_dyn ]
  trait Trait1
  {
  }

  let vec = Vec::< Box< dyn Trait1 > >::new();
  let vec2 = vec.clone(); /* <- it does not work without `clone_dyn` */
# }

To add to your project

cargo add clone_dyn

Try out from the repository

git clone https://github.com/Wandalen/wTools
cd wTools
cd examples/clone_dyn_trivial
cargo run

Dependencies

~0.4–0.8MB
~20K SLoC