LightClone
Compile-time enforcement for O(1) clone operations in Rust.
Overview
LightClone provides a marker trait and derive macro that guarantees cloning is cheap. It only allows types where cloning involves:
- Atomic refcount increments (
Arc) - Non-atomic refcount increments (
Rc) - Bitwise copies (
Copytypes) - Persistent data structures (im, imbl, rpds with structural sharing)
Types like String, Vec, or HashMap that perform deep copies are rejected at compile time.
Usage
use LightClone;
use Arc;
let config = Config ;
// .light_clone() or .lc() for short
let clone = config.lc;
Compile-Time Safety
Invalid types are caught at compile time:
Ergonomic Strings
Use LightStr as a cheap-to-clone string type:
use ;
let s: LightStr = "hello".into_light_str;
let clone = s.lc; // O(1) - just increments refcount
Features
Optional integrations with persistent collection libraries:
[]
= { = "0.1", = ["imbl"] }
| Feature | Crate | Description |
|---|---|---|
im |
im | Immutable collections |
imbl |
imbl | Maintained fork of im |
rpds |
rpds | Reactive persistent data structures |
full |
All of the above | Enable all collection integrations |
Supported Types
Primitives
All primitive types: i8-i128, u8-u128, f32, f64, bool, char, ()
Smart Pointers
Arc<T>whereT: ?SizedRc<T>whereT: ?Sized
Containers
Option<T>whereT: LightCloneResult<T, E>whereT: LightClone, E: LightClonePhantomData<T>- Tuples up to 12 elements
Enums
Performance
LightClone has zero runtime overhead. The .light_clone() method compiles to identical code as .clone() for the underlying types.
Benchmarks show 5-19x speedup compared to deep cloning when using persistent collections instead of standard library collections.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.