1515 - ` Owned ` - Heap-allocated owned data via ` Box<T> `
1616 - ` Shared ` - ` Arc<T> ` for shared immutable data
1717 - ` Updatable ` - Lock-free atomic updates using ` arc-swap `
18+ - ` Lazy ` - Lazy initialization with atomic updates for static contexts
1819
19- - ** Lock-Free Updates** : The ` Updatable ` variant uses ` arc-swap ` for atomic, lock-free updates
20+ - ** Lock-Free Updates** : The ` Updatable ` and ` Lazy ` variants use ` arc-swap ` for atomic, lock-free updates
21+ - ** Lazy Initialization** : The ` Lazy ` variant makes it possbile to create an ` Updatable ` in a static context
2022- ** Flexible API** : Easy conversion between different storage types
2123- ** Zero-Cost Abstractions** : Minimal overhead for common operations
22- - ** Thread-Safe** : Share data safely across threads with ` Shared ` and ` Updatable ` variants
24+ - ** Thread-Safe** : Share data safely across threads with ` Shared ` , ` Updatable ` , and ` Lazy ` variants
2325
2426## 📦 Installation
2527
@@ -35,6 +37,7 @@ anycow = "0.1"
3537AnyCow shines in scenarios where you have:
3638
3739- ** Configuration data** that's read frequently but updated occasionally
40+ - ** Global/static data** that needs lazy initialization and atomic updates
3841- ** Cached values** that need atomic updates without locks
3942- ** Shared state** across multiple threads with infrequent modifications
4043- ** Hot paths** where you want to minimize allocation overhead
@@ -50,13 +53,15 @@ let borrowed = AnyCow::borrowed(&"hello");
5053let owned = AnyCow :: owned (String :: from (" world" ));
5154let shared = AnyCow :: shared (std :: sync :: Arc :: new (42 ));
5255let updatable = AnyCow :: updatable (vec! [1 , 2 , 3 ]);
56+ let lazy = AnyCow :: lazy (|| vec! [7 , 8 , 9 ]);
5357
5458// Read values efficiently
5559println! (" {}" , * borrowed . borrow ()); // "hello"
5660println! (" {}" , * owned . borrow ()); // "world"
5761
5862// Atomic updates (lock-free!)
5963updatable . try_replace (vec! [4 , 5 , 6 ]). unwrap ();
64+ lazy . try_replace (vec! [10 , 11 , 12 ]). unwrap ();
6065```
6166
6267## 💡 Examples
@@ -130,6 +135,37 @@ cache.try_replace(vec![4, 5, 6, 7, 8]).unwrap();
130135reader . join (). unwrap ();
131136```
132137
138+ ### Lazy Global Configuration
139+
140+ ``` rust
141+ use anycow :: AnyCow ;
142+ use std :: collections :: HashMap ;
143+
144+ // Perfect for static/global data that's expensive to initialize
145+ static CONFIG : AnyCow <HashMap <String , String >> = AnyCow :: lazy (|| {
146+ println! (" Loading configuration..." ); // Only runs once!
147+ let mut config = HashMap :: new ();
148+ config . insert (" app_name" . to_string (), " MyApp" . to_string ());
149+ config . insert (" version" . to_string (), " 1.0.0" . to_string ());
150+ config
151+ });
152+
153+ fn main () {
154+ // First access initializes the config
155+ let app_name = CONFIG . borrow (). get (" app_name" ). cloned (). unwrap ();
156+ println! (" App: {}" , app_name );
157+
158+ // Subsequent accesses are fast (no re-initialization)
159+ let version = CONFIG . borrow (). get (" version" ). cloned (). unwrap ();
160+ println! (" Version: {}" , version );
161+
162+ // Update the global config atomically
163+ let mut new_config = HashMap :: new ();
164+ new_config . insert (" app_name" . to_string (), " MyApp Pro" . to_string ());
165+ new_config . insert (" version" . to_string (), " 2.0.0" . to_string ());
166+ CONFIG . try_replace (new_config ). unwrap ();
167+ }
168+
133169## 🧠 Storage Strategy Guide
134170
135171| Variant | Best For | Thread Safe | Mutable | Memory |
@@ -138,6 +174,7 @@ reader.join().unwrap();
138174| `Owned ` | Exclusive ownership | ❌ | ✅ | Heap |
139175| `Shared ` | Read - only sharing | ✅ | ❌ | Shared |
140176| `Updatable ` | Concurrent reads + atomic updates | ✅ | Via `try_replace ()` | Shared + Atomic |
177+ | `Lazy ` | Static / global data + atomic updates | ✅ | Via `try_replace ()` | Lazy + Shared + Atomic |
141178
142179## 🔧 API Reference
143180
@@ -147,6 +184,7 @@ AnyCow::borrowed(&value) // From reference
147184AnyCow :: owned (value ) // From owned value (boxed)
148185AnyCow :: shared (arc ) // From Arc<T>
149186AnyCow :: updatable (value ) // Create updatable variant
187+ AnyCow :: lazy (init_fn ) // Create lazy variant with init function
150188```
151189
152190### Access
@@ -159,15 +197,16 @@ container.to_arc() // Convert to Arc<T>
159197
160198### Updates
161199``` rust
162- container . try_replace (new_value ) // Atomic update (Updatable only)
200+ container . try_replace (new_value ) // Atomic update (Updatable & Lazy only)
163201```
164202
165203## ⚡ Performance
166204
167205AnyCow is designed for performance:
168206
169207- ** Zero-cost borrowing** : No allocation for ` Borrowed ` variant
170- - ** Lock-free updates** : ` Updatable ` uses ` arc-swap ` for atomic operations
208+ - ** Lazy initialization** : ` Lazy ` variant allow to create an ` Updatable ` in a static context
209+ - ** Lock-free updates** : ` Updatable ` and ` Lazy ` use ` arc-swap ` for atomic operations
171210- ** Minimal overhead** : Smart enum design with efficient memory layout
172211- ** Branch prediction friendly** : Common operations are optimized
173212
0 commit comments