overlay-map
overlay-map is a two-layered map data structure for Rust that allows for non-destructive updates by maintaining both a foreground (mutable) and a background (read-only) value layer for each key.
Itβs designed for scenarios where you want to:
- Apply temporary or just-in-time changes without mutating original data.
- Implement overlays, deltas, rollback-able changes, or speculative state changes.
- Avoid cloning/copying values unnecessarily on update.
β οΈ Work in progress: This library is still evolving. Planned features include thread-safe version, rollback support, and a wider API.
π¦ Features
- β Foreground and background storage per key
- β On insert, the old foreground is pushed to background (only one previous value is retained β not a full history)
- β If a key exists, its foreground is always present β no fallback logic is required during reads
- β Zero-cost value pushing (via in-place pointer tricks)
- β No cloning required on push
- β
Optional conditional pushes (
push_if) - β Extendable from other maps
π Example
use OverlayMap;
/// Simulates measurement collapse
π§ Why?
This is useful when:
- You want to track current vs previous state (not full history).
- You're doing speculative updates or rollback systems (planned).
- You need non-destructive overlays (e.g. config layering, versioning, etc).
- You want to avoid expensive cloning when replacing data.
π Documentation
π License
MIT
β¨ Contributing
Contributions, bug reports, and feature requests welcome.
Planned areas of work:
- Thread-safe version
- Rollback support
- More expressive APIs