🐄 AnyCow
A supercharged container for read-heavy, occasionally-updated data structures
AnyCow is a versatile, high-performance container that extends the concept of Cow (Clone-on-Write) with multiple storage strategies optimized for different use cases. Perfect for scenarios where you need to read values frequently but update them only occasionally.
🚀 Features
-
Multiple Storage Strategies: Choose the right storage for your use case
Borrowed- Zero-cost references for temporary dataOwned- Heap-allocated owned data viaBox<T>Shared-Arc<T>for shared immutable dataUpdatable- Lock-free atomic updates usingarc-swapLazy- Lazy initialization with atomic updates for static contexts
-
Lock-Free Updates: The
UpdatableandLazyvariants usearc-swapfor atomic, lock-free updates -
Lazy Initialization: The
Lazyvariant makes it possbile to create anUpdatablein a static context -
Flexible API: Easy conversion between different storage types
-
Zero-Cost Abstractions: Minimal overhead for common operations
-
Thread-Safe: Share data safely across threads with
Shared,Updatable, andLazyvariants
📦 Installation
Add this to your Cargo.toml:
[]
= "0.1"
🎯 Use Cases
AnyCow shines in scenarios where you have:
- Configuration data that's read frequently but updated occasionally
- Global/static data that needs lazy initialization and atomic updates
- Cached values that need atomic updates without locks
- Shared state across multiple threads with infrequent modifications
- Hot paths where you want to minimize allocation overhead
- APIs that need to accept both borrowed and owned data flexibly
🔥 Quick Start
use AnyCow;
// Create from different sources
let borrowed = borrowed;
let owned = owned;
let shared = shared;
let updatable = updatable;
let lazy = lazy;
// Read values efficiently
println!; // "hello"
println!; // "world"
// Atomic updates (lock-free!)
updatable.try_replace.unwrap;
lazy.try_replace.unwrap;
💡 Examples
Reading Configuration
use AnyCow;
use Arc;
// Create updatable config
let config = updatable;
// Read frequently (very fast)
let current_config = config.borrow;
println!;
// Update occasionally (atomic, lock-free)
config.try_replace.unwrap;
Flexible API Design
use AnyCow;
// All of these work!
process_data;
process_data;
process_data;
Cache with Atomic Updates
use AnyCow;
use thread;
use Arc;
let cache = new;
// Spawn reader threads
let cache_clone = cache.clone;
let reader = spawn;
// Update cache atomically
cache.try_replace.unwrap;
reader.join.unwrap;
Lazy Global Configuration
use AnyCow;
use HashMap;
// Perfect for static/global data that's expensive to initialize
static CONFIG: = lazy;
## 🧠 Storage Strategy Guide
| Variant | Best For | Thread Safe | Mutable | Memory |
|---------|----------|-------------|---------|--------|
| `Borrowed` | Temporary refs, hot paths | ❌ | ❌ | Zero-copy |
| `Owned` | Exclusive ownership | ❌ | ✅ | Heap |
| `Shared` | Read-only sharing | ✅ | ❌ | Shared |
| `Updatable` | Concurrent reads + atomic updates | ✅ | Via `try_replace` | Shared + Atomic |
| `Lazy` | Static/global data + atomic updates | ✅ | Via `try_replace` | Lazy + Shared + Atomic |
## 🔧 API Reference
### Construction
```rust
borrowed // From reference
owned // From owned value (boxed)
shared // From Arc<T>
updatable // Create updatable variant
lazy // Create lazy variant with init function
Access
container.borrow // Get reference to value
container.to_mut // Get mutable reference (COW)
container.into_owned // Convert to owned value
container.to_arc // Convert to Arc<T>
Updates
container.try_replace // Atomic update (Updatable & Lazy only)
⚡ Performance
AnyCow is designed for performance:
- Zero-cost borrowing: No allocation for
Borrowedvariant - Lazy initialization:
Lazyvariant allow to create anUpdatablein a static context - Lock-free updates:
UpdatableandLazyusearc-swapfor atomic operations - Minimal overhead: Smart enum design with efficient memory layout
- Branch prediction friendly: Common operations are optimized
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🙏 Acknowledgments
- Built with
arc-swapfor lock-free atomic operations - Inspired by the standard library's
Cowbut supercharged for modern use cases
Made with ❤️ for the Rust community. Happy coding! 🦀