/implicit-clone

Immutable types and ImplicitClone trait similar to Copy

Primary LanguageRust

Rust Latest Version License Docs.rs LOC Dependency Status

ImplicitClone

A library that introduces the marker trait ImplicitClone which allows reproducing the behavior of the trait Copy but calls the Clone implementation instead and must be implemented in the host library.

The idea is that you must implement this trait on types that are cheap to clone (std::rc::Rc and std::sync::Arc types are automatically implemented). Then the host library must use the trait ImplicitClone to allow their users to pass objects that will be cloned automatically.

This crate is in the category rust-patterns but this is actually a Rust anti-pattern. In Rust the user should always handle borrowing and ownership by themselves. Nevertheless, this pattern is sometimes desirable. For example, UI frameworks that rely on propagating properties from ancestors to children will always need to use Rc'ed types to allow every child component to update. This is the case in React-like framework like Yew.

This crates also provide a few convenient immutable types for handling cheap-to-clone string, array and maps which you can find in the modules sync and unsync. Those types implement ImplicitClone and hold only types that implement ImplicitClone as well. One big particularity: iterating on these types yields clones of the items and not references. This can be particularly handy when using a React-like framework.