#immutability #copy #rc #cheap-clone

implicit-clone

Immutable types and ImplicitClone trait similar to Copy

20 releases

0.5.0 Jul 12, 2024
0.4.9 Feb 21, 2024
0.4.8 Nov 19, 2023
0.4.0 Jul 29, 2023
0.3.2 Jul 12, 2022

#161 in Rust patterns

Download history 7242/week @ 2024-08-19 8193/week @ 2024-08-26 7219/week @ 2024-09-02 7475/week @ 2024-09-09 6247/week @ 2024-09-16 8265/week @ 2024-09-23 9314/week @ 2024-09-30 6426/week @ 2024-10-07 8207/week @ 2024-10-14 8677/week @ 2024-10-21 7487/week @ 2024-10-28 7364/week @ 2024-11-04 6010/week @ 2024-11-11 6983/week @ 2024-11-18 8083/week @ 2024-11-25 7224/week @ 2024-12-02

28,810 downloads per month
Used in 185 crates (10 directly)

MIT/Apache

59KB
1K SLoC

Rust Latest Version License Docs.rs LOC Dependency Status

ImplicitClone

This library introduces the marker trait ImplicitClone intended for cheap-to-clone types that should be allowed to be cloned implicitly. It enables host libraries using this crate to have the syntax of Copy while actually calling the Clone implementation instead (usually when host library does such syntax in a macro).

The idea is that you must implement this trait on your cheap-to-clone types, and then the host library using the trait will allow users to pass values of your types and they will be cloned automatically.

Standard types that the ImplicitClone is already implemented for:

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 multiple children will always need to use Rc'd types to cheaply and concisely update every child component. This is the case in React-like frameworks like Yew.

This crate also provides a few convenient immutable types for handling cheap-to-clone strings, arrays and maps, you can find them 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.

Dependencies

~0–320KB