Skip to main content

DeepClone

Derive Macro DeepClone 

Source
#[derive(DeepClone)]
{
    // Attributes available to this derive:
    #[deepclone]
}
Expand description

Derive DeepClone, cloning every field through the same Cloner.

Structs (named, tuple, and unit), enums, generics, and where-clauses are supported; unions are not. Every type parameter gains a DeepClone bound, as derive(Clone) adds a Clone bound.

Nothing here inspects field types: Rc and Arc reach the cloner through their own DeepClone impls, so Vec<Rc<T>> and HashMap<K, Rc<T>> work too, which a derive matching on the literal token Rc would miss.

No 'static bound is added, so a generic type with an Rc<..T..> field needs T: 'static on its own declaration.

§Field attributes

  • #[deepclone(clone)] — use Clone::clone. Correct for immutable or unshared data, never for an Rc you want independent.
  • #[deepclone(with = path)] — call path(&field, cloner).
  • #[deepclone(default)] — ignore the source value and use Default::default().

§Container attributes

  • #[deepclone(bound = "T: MyBound")] — replace the generated bounds, for when a DeepClone bound on every parameter is too strong.