Note record
LightClone
- Record
- Note / / 1 min read / 202 words
On this page4 sections / +
TL;DR - A Rust derive macro that enforces cheap clones at compile time.
Related posts can be found under tag light-clone
What It Does
LightClone is a trait and derive macro that only allows types that are cheap to clone:
- Copy types - primitives, bitwise copies
- Refcounted -
Arc<T>,Rc<T>- just bump a counter - Persistent collections -
imbl,rpds- structural sharing, O(1) clone
If you derive LightClone on a struct with an expensive field (like String), your code won't compile.
Why I Built It
In Rust, cheap and expensive clones look identical - both use .clone(). When writing High Level Rust with liberal cloning, it's easy to accidentally add an expensive field during refactoring and tank performance with zero compiler warnings. And expensive clones in Rust are very expensive.
LightClone turns invisible performance footguns into compiler errors.
How It's Built
A marker trait that requires Clone, plus a derive macro that validates all fields implement LightClone at compile time. The actual cloning delegates to .clone() - the trait just provides compile-time safety.
Feature flags enable support for ecosystem crates: imbl, rpds, bytes, smol_str, uuid, chrono, and more.