Crates.io | smart-clone |
lib.rs | smart-clone |
version | 0.1.0 |
source | src |
created_at | 2024-07-19 10:58:13.983843 |
updated_at | 2024-07-19 10:58:13.983843 |
description | Rust custom-derive macro for Clone with more control on the fields |
homepage | |
repository | https://github.com/dclause/rust-smart-clone |
max_upload_size | |
id | 1308530 |
size | 27,507 |
Rust custom-derive macro for implementing Clone
trait with more control on the fields cloned values.
use smart_clone::SmartClone;
#[derive(SmartClone)]
struct Foo {
a: u8, // left for standard clone
#[clone = 12]
b: u8, // Override with hardcoded value (same as `#[clone(12)]`)
#[clone(TEST)]
c: u8, // In general, prefer this syntax for hardcode, variable or const
#[clone((42, 69))]
d: (i32, u32),
#[clone(default)]
e: Vec<Vec<Vec<(u8, u8)>>>, // Reserved 'skip' keyword to clone to Default::default() value (g type must implement `Default`)
#[clone(Some(Default::default()))] // `Some(Default::default())` is not `None` but `Some(0)` !
f: Option<i32>,
#[clone(clone_with = "SimpleStruct::vec_clone")]
g: Vec<u32>,
#[clone("banana".to_owned())]
h: String,
}
Will generate:
impl Clone for SimpleStruct {
fn clone(&self) -> Self {
Self {
a: self.a.clone(),
b: 12,
c: TEST,
d: (42, 69),
e: Default::default(),
f: Some(Default::default()),
g: SimpleStruct::vec_clone(&self.g),
h: "banana".to_owned(),
}
}
}
smart-clone = "0.1.0"
as a dependency in Cargo.toml.use smart_clone::SmartClone;
within the same
module and write #[derive(SmartClone)]
on the struct or enum.#[clone(...)]
with the option you need on the desired structure field.#[clone]
: will perform cloning as usual for your field. Equivalent to no annotation.#[clone = xxx]
: will set the value xxx
to the field when the structure is cloned#[clone(xxx)]
: same as above, but xxx
can be whatever you want here, not just a literal#[clone(clone_with = "xxx")]
: the field will be passed by reference to a function called xxx
and the
returned value will be used when the structure is cloned.See the examples folder for various use cases.