Rust SmartClone
Rust custom-derive macro for implementing Clone trait with more control on the fields cloned values.
use SmartClone;
Will generate:
Install
- Add
smart-clone = "0.1.0"as a dependency in Cargo.toml. - On structs and enums that you want to clone, import the derive macro as
use smart_clone::SmartClone;within the same module and write#[derive(SmartClone)]on the struct or enum. - Use the attribute
#[clone(...)]with the option you need on the desired structure field.
API options
#[clone]: will perform cloning as usual for your field. Equivalent to no annotation.#[clone = xxx]: will set the valuexxxto the field when the structure is cloned#[clone(xxx)]: same as above, butxxxcan be whatever you want here, not just a literal#[clone(clone_with = "xxx")]: the field will be passed by reference to a function calledxxxand the returned value will be used when the structure is cloned.
Examples
See the examples folder for various use cases.