thread-share 0.1.6

A Rust library for safe and efficient data sharing between threads with zero-copy operations, change detection, and enhanced thread management.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//! # Enhanced Module - EnhancedThreadShare<T>
//!
//! This module provides `EnhancedThreadShare<T>`, a powerful extension of `ThreadShare<T>`
//! that adds automatic thread management capabilities.
//!
//! ## 🚀 Overview
//!
//! `EnhancedThreadShare<T>` eliminates the need for manual thread management by providing:
//!
//! - **Automatic Thread Spawning**: Spawn threads with a single method call
//! - **Built-in Thread Tracking**: Monitor active thread count and status
//! - **Automatic Thread Joining**: Wait for all threads to complete with `join_all()`
//! - **Thread Naming**: Give meaningful names to threads for debugging
//! - **All ThreadShare Features**: Inherits all capabilities from `ThreadShare<T>`
//!
//! ## Key Benefits
//!
//! ### 🎯 Simplified Thread Management
//! ```rust
//! // Old way: Manual thread management
//! use thread_share::share;
//! use std::thread;
//!
//! let data = share!(vec![1, 2, 3]);
//! let clone1 = data.clone();
//! let clone2 = data.clone();
//!
//! let handle1 = thread::spawn(move || { /* logic */ });
//! let handle2 = thread::spawn(move || { /* logic */ });
//!
//! handle1.join().expect("Failed to join");
//! handle2.join().expect("Failed to join");
//!
//! // New way: Enhanced thread management
//! use thread_share::enhanced_share;
//!
//! let enhanced = enhanced_share!(vec![1, 2, 3]);
//!
//! enhanced.spawn("worker1", |data| { /* logic */ });
//! enhanced.spawn("worker2", |data| { /* logic */ });
//!
//! enhanced.join_all().expect("Failed to join");
//! ```
//!
//! ### 📊 Real-time Monitoring
//! ```rust
//! use thread_share::enhanced_share;
//!
//! let enhanced = enhanced_share!(vec![1, 2, 3]);
//!
//! enhanced.spawn("processor", |data| { /* logic */ });
//! enhanced.spawn("validator", |data| { /* logic */ });
//!
//! println!("Active threads: {}", enhanced.active_threads());
//!
//! // Wait for completion
//! enhanced.join_all().expect("Failed to join");
//!
//! assert!(enhanced.is_complete());
//! ```
//!
//! ## Architecture
//!
//! `EnhancedThreadShare<T>` wraps a `ThreadShare<T>` and adds:
//!
//! - **`inner: ThreadShare<T>`** - The underlying shared data
//! - **`threads: Arc<Mutex<HashMap<String, JoinHandle<()>>>>`** - Thread tracking
//!
//! ## Thread Lifecycle
//!
//! 1. **Creation**: `EnhancedThreadShare::new(data)` or `enhanced_share!(data)`
//! 2. **Spawning**: `enhanced.spawn(name, function)` creates named threads
//! 3. **Execution**: Threads run with access to shared data
//! 4. **Monitoring**: Track active threads with `active_threads()`
//! 5. **Completion**: Wait for all threads with `join_all()`
//!
//! ## Example Usage
//!
//! ### Basic Thread Management
//! ```rust
//! use thread_share::{enhanced_share, spawn_workers};
//!
//! let data = enhanced_share!(vec![1, 2, 3]);
//!
//! // Spawn individual threads
//! data.spawn("sorter", |data| {
//!     data.update(|v| v.sort());
//! });
//!
//! data.spawn("validator", |data| {
//!     assert!(data.get().is_sorted());
//! });
//!
//! // Wait for completion
//! data.join_all().expect("Failed to join");
//! ```
//!
//! ### Using Macros
//! ```rust
//! use thread_share::share;
//!
//! let data = share!(String::from("Hello"));
//! let clone = data.clone();
//!
//! // Spawn a simple thread
//! std::thread::spawn(move || {
//!     clone.update(|s| s.push_str(" World"));
//! });
//!
//! // Wait a bit and check result
//! std::thread::sleep(std::time::Duration::from_millis(100));
//! println!("Updated: {}", data.get());
//! ```
//!
//! ### Real-world Example
//! ```rust
//! use thread_share::share;
//! use std::time::Duration;
//!
//! #[derive(Clone)]
//! struct Server {
//!     port: u16,
//!     is_running: bool,
//!     connections: u32,
//! }
//!
//! let server = share!(Server {
//!     port: 8080,
//!     is_running: false,
//!     connections: 0,
//! });
//!
//! let server_clone = server.clone();
//!
//! // Spawn a simple server thread
//! std::thread::spawn(move || {
//!     server_clone.update(|s| {
//!         s.is_running = true;
//!         s.connections = 5;
//!     });
//! });
//!
//! // Wait a bit and check result
//! std::thread::sleep(Duration::from_millis(100));
//! let final_state = server.get();
//! println!("Server running: {}, connections: {}", final_state.is_running, final_state.connections);
//! ```
//!
//! ## Performance Characteristics
//!
//! - **Thread Spawning**: Minimal overhead over standard `thread::spawn`
//! - **Thread Tracking**: Constant-time operations for thread management
//! - **Memory Usage**: Small overhead for thread tracking structures
//! - **Scalability**: Efficient for up to hundreds of threads
//!
//! ## Best Practices
//!
//! 1. **Use descriptive thread names** for easier debugging
//! 2. **Keep thread functions focused** on single responsibilities
//! 3. **Always call `join_all()`** to ensure proper cleanup
//! 4. **Monitor thread count** with `active_threads()` for debugging
//! 5. **Handle errors gracefully** from `join_all()` and `spawn()`
//!
//! ## Error Handling
//!
//! ```rust
//! use thread_share::share;
//!
//! let data = share!(0);
//! let clone = data.clone();
//!
//! // Spawn thread with error handling
//! let handle = std::thread::spawn(move || {
//!     clone.update(|x| *x = *x + 1);
//! });
//!
//! // Handle join errors
//! if let Err(e) = handle.join() {
//!     eprintln!("Thread execution failed: {:?}", e);
//! }
//! ```
//!
//! ## Thread Safety
//!
//! `EnhancedThreadShare<T>` automatically implements `Send` and `Sync` traits
//! when `T` implements them, making it safe to use across thread boundaries.
//!
//! ## Integration with Macros
//!
//! This module works seamlessly with the library's macros:
//!
//! - **`enhanced_share!`** - Creates `EnhancedThreadShare<T>` instances
//! - **`spawn_workers!`** - Spawns multiple threads with single macro call
//!
//! ## Comparison with Manual Thread Management
//!
//! | Aspect | Manual Management | EnhancedThreadShare |
//! |--------|-------------------|-------------------|
//! | **Thread Creation** | `thread::spawn()` calls | `enhanced.spawn()` |
//! | **Thread Tracking** | Manual `JoinHandle` storage | Automatic tracking |
//! | **Thread Joining** | Manual `join()` calls | `join_all()` |
//! | **Error Handling** | Per-thread error handling | Centralized error handling |
//! | **Debugging** | No thread identification | Named threads |
//! | **Code Complexity** | High | Low |

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::thread::{self};
use crate::core::ThreadShare;

#[cfg(feature = "serialize")]
use serde::{Serialize, Deserialize};

/// Enhanced ThreadShare with built-in thread management
///
/// `EnhancedThreadShare<T>` extends `ThreadShare<T>` with automatic thread management
/// capabilities, eliminating the need for manual thread spawning and joining.
///
/// ## Key Features
///
/// - **Automatic Thread Spawning**: Spawn threads with a single method call
/// - **Built-in Thread Tracking**: Monitor active thread count and status
/// - **Automatic Thread Joining**: Wait for all threads to complete with `join_all()`
/// - **Thread Naming**: Give meaningful names to threads for debugging
/// - **All ThreadShare Features**: Inherits all capabilities from `ThreadShare<T>`
///
/// ## Example
///
/// ```rust
/// use thread_share::{enhanced_share, spawn_workers};
///
/// let data = enhanced_share!(vec![1, 2, 3]);
///
/// // Spawn individual threads
/// data.spawn("sorter", |data| {
///     data.update(|v| v.sort());
/// });
///
/// data.spawn("validator", |data| {
///     assert!(data.get().is_sorted());
/// });
///
/// // Wait for completion
/// data.join_all().expect("Failed to join");
/// ```
///
/// ## Thread Lifecycle
///
/// 1. **Creation**: `EnhancedThreadShare::new(data)` or `enhanced_share!(data)`
/// 2. **Spawning**: `enhanced.spawn(name, function)` creates named threads
/// 3. **Execution**: Threads run with access to shared data
/// 4. **Monitoring**: Track active threads with `active_threads()`
/// 5. **Completion**: Wait for all threads with `join_all()`
///
/// ## Performance
///
/// - **Thread Spawning**: Minimal overhead over standard `thread::spawn`
/// - **Thread Tracking**: Constant-time operations for thread management
/// - **Memory Usage**: Small overhead for thread tracking structures
/// - **Scalability**: Efficient for up to hundreds of threads
pub struct EnhancedThreadShare<T> {
    inner: ThreadShare<T>,
    threads: Arc<Mutex<HashMap<String, thread::JoinHandle<()>>>>,
}

impl<T> EnhancedThreadShare<T> {
    /// Creates a new EnhancedThreadShare
    ///
    /// This method creates a new `EnhancedThreadShare<T>` instance with the provided data.
    /// The data is wrapped in a `ThreadShare<T>` for safe sharing between threads.
    ///
    /// ## Arguments
    ///
    /// * `data` - The initial data to share between threads
    ///
    /// ## Returns
    ///
    /// A new `EnhancedThreadShare<T>` instance.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use thread_share::EnhancedThreadShare;
    ///
    /// let enhanced = EnhancedThreadShare::new(0);
    /// let enhanced = EnhancedThreadShare::new(String::from("Hello"));
    /// let enhanced = EnhancedThreadShare::new(vec![1, 2, 3]);
    /// ```
    pub fn new(data: T) -> Self {
        Self {
            inner: ThreadShare::new(data),
            threads: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Spawns a thread with access to this shared data
    ///
    /// This method creates a new thread with the given name and function.
    /// The thread receives a clone of the shared data and can safely modify it.
    ///
    /// ## Arguments
    ///
    /// * `name` - A descriptive name for the thread (useful for debugging)
    /// * `f` - A function that receives `ThreadShare<T>` and performs the thread's work
    ///
    /// ## Requirements
    ///
    /// The function `F` must:
    /// - Implement `FnOnce(ThreadShare<T>)` - called once with shared data
    /// - Implement `Send` - safe to send across thread boundaries
    /// - Have `'static` lifetime - no borrowed references
    ///
    /// The type `T` must implement `Send + Sync + 'static`.
    ///
    /// ## Returns
    ///
    /// `Ok(())` on success, `Err(String)` if thread spawning fails.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use thread_share::EnhancedThreadShare;
    ///
    /// let enhanced = EnhancedThreadShare::new(0);
    ///
    /// // Spawn a worker thread
    /// enhanced.spawn("worker", |data| {
    ///     for _ in 0..100 {
    ///         data.update(|x| *x += 1);
    ///         std::thread::sleep(std::time::Duration::from_millis(10));
    ///     }
    /// }).expect("Failed to spawn worker");
    ///
    /// // Spawn a monitor thread
    /// enhanced.spawn("monitor", |data| {
    ///     for _ in 0..10 {
    ///         std::thread::sleep(std::time::Duration::from_millis(100));
    ///         println!("Current value: {}", data.get());
    ///     }
    /// }).expect("Failed to spawn monitor");
    /// ```
    pub fn spawn<F>(&self, name: &str, f: F) -> Result<(), String>
    where
        F: FnOnce(ThreadShare<T>) + Send + 'static,
        T: Send + Sync + 'static,
    {
        let thread_name = name.to_string();
        let thread_data = self.inner.clone();

        let handle = thread::spawn(move || {
            f(thread_data);
        });

        self.threads.lock().unwrap().insert(thread_name, handle);
        Ok(())
    }

    /// Spawns multiple threads with different names and functions
    ///
    /// This method spawns multiple threads from a vector of configurations.
    /// Each configuration contains a thread name and a function.
    ///
    /// ## Arguments
    ///
    /// * `thread_configs` - Vector of `(name, function)` tuples
    ///
    /// ## Requirements
    ///
    /// The function `F` must implement `Clone` in addition to the standard requirements.
    ///
    /// ## Returns
    ///
    /// `Ok(())` on success, `Err(String)` if any thread spawning fails.
    /// ```
    pub fn spawn_multiple<F>(&self, thread_configs: Vec<(&str, F)>) -> Result<(), String>
    where
        F: FnOnce(ThreadShare<T>) + Send + Clone + 'static,
        T: Send + Sync + 'static,
    {
        for (name, func) in thread_configs {
            self.spawn(name, func)?;
        }
        Ok(())
    }

    /// Spawns multiple threads with boxed closures
    ///
    /// This method spawns multiple threads using boxed closures, which allows
    /// for different function types in the same vector.
    ///
    /// ## Arguments
    ///
    /// * `thread_configs` - Vector of `(name, boxed_function)` tuples
    ///
    /// ## Returns
    ///
    /// `Ok(())` on success, `Err(String)` if any thread spawning fails.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use thread_share::EnhancedThreadShare;
    ///
    /// let enhanced = EnhancedThreadShare::new(0);
    ///
    /// let configs = vec![
    ///     ("worker1", Box::new(|data: thread_share::ThreadShare<i32>| { data.update(|x| *x = *x + 1); }) as Box<dyn FnOnce(thread_share::ThreadShare<i32>) + Send>),
    ///     ("worker2", Box::new(|data: thread_share::ThreadShare<i32>| { data.update(|x| *x = *x + 2); }) as Box<dyn FnOnce(thread_share::ThreadShare<i32>) + Send>),
    /// ];
    ///
    /// enhanced.spawn_multiple_boxed(configs).expect("Failed to spawn threads");
    /// ```
    pub fn spawn_multiple_boxed(
        &self,
        thread_configs: Vec<(&str, Box<dyn FnOnce(ThreadShare<T>) + Send>)>,
    ) -> Result<(), String>
    where
        T: Send + Sync + 'static,
    {
        for (name, func) in thread_configs {
            let thread_data = self.inner.clone();
            let handle = thread::spawn(move || {
                func(thread_data);
            });
            self.threads
                .lock()
                .unwrap()
                .insert(name.to_string(), handle);
        }
        Ok(())
    }

    /// Waits for all spawned threads to complete
    ///
    /// This method blocks until all spawned threads have finished execution.
    /// It joins each thread and returns an error if any thread panics.
    ///
    /// ## Returns
    ///
    /// `Ok(())` when all threads complete successfully, `Err(String)` if any thread fails.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use thread_share::EnhancedThreadShare;
    ///
    /// let enhanced = EnhancedThreadShare::new(0);
    ///
    /// enhanced.spawn("worker", |data| {
    ///     data.update(|x| *x = *x + 100);
    /// }).expect("Failed to spawn worker");
    ///
    /// // Wait for all threads to complete
    /// enhanced.join_all().expect("Thread execution failed");
    ///
    /// // Now safe to access the final result
    /// assert_eq!(enhanced.get(), 100);
    /// ```
    pub fn join_all(&self) -> Result<(), String> {
        let mut threads = self.threads.lock().unwrap();
        let thread_handles: Vec<_> = threads.drain().collect();
        drop(threads);

        for (name, handle) in thread_handles {
            let result = handle.join();
            if let Err(e) = result {
                return Err(format!("Thread '{}' failed: {:?}", name, e));
            }
        }
        Ok(())
    }

    /// Gets the number of active threads
    ///
    /// This method returns the current number of threads that are still running.
    ///
    /// ## Returns
    ///
    /// The number of active threads.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use thread_share::EnhancedThreadShare;
    /// use std::time::Duration;
    ///
    /// let enhanced = EnhancedThreadShare::new(0);
    ///
    /// enhanced.spawn("worker", |data| {
    ///     std::thread::sleep(Duration::from_millis(100));
    /// }).expect("Failed to spawn worker");
    ///
    /// println!("Active threads: {}", enhanced.active_threads()); // Prints: 1
    ///
    /// // Wait for completion
    /// enhanced.join_all().expect("Failed to join");
    ///
    /// println!("Active threads: {}", enhanced.active_threads()); // Prints: 0
    /// ```
    pub fn active_threads(&self) -> usize {
        self.threads.lock().unwrap().len()
    }

    /// Checks if all threads have completed
    ///
    /// This method returns `true` if there are no active threads, `false` otherwise.
    ///
    /// ## Returns
    ///
    /// `true` if all threads have completed, `false` if any threads are still running.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use thread_share::EnhancedThreadShare;
    ///
    /// let enhanced = EnhancedThreadShare::new(0);
    ///
    /// enhanced.spawn("worker", |data| {
    ///     data.update(|x| *x = *x + 1);
    /// }).expect("Failed to spawn worker");
    ///
    /// assert!(!enhanced.is_complete()); // Thread is still running
    ///
    /// enhanced.join_all().expect("Failed to join");
    ///
    /// assert!(enhanced.is_complete()); // All threads completed
    /// ```
    pub fn is_complete(&self) -> bool {
        self.threads.lock().unwrap().is_empty()
    }

    /// Gets a reference to the threads HashMap for external management
    pub fn get_threads(&self) -> Arc<Mutex<HashMap<String, thread::JoinHandle<()>>>> {
        self.threads.clone()
    }

    /// Delegates all ThreadShare methods
    ///
    /// Gets a copy of the shared data.
    ///
    /// ## Requirements
    ///
    /// The type `T` must implement `Clone` trait.
    ///
    /// ## Returns
    ///
    /// A copy of the current data.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use thread_share::EnhancedThreadShare;
    ///
    /// let enhanced = EnhancedThreadShare::new(42);
    /// let value = enhanced.get();
    /// assert_eq!(value, 42);
    /// ```
    pub fn get(&self) -> T
    where
        T: Clone,
    {
        self.inner.get()
    }

    /// Sets new data and notifies waiting threads
    ///
    /// This method replaces the current data and notifies all threads
    /// waiting for changes.
    ///
    /// ## Arguments
    ///
    /// * `new_data` - The new data to set
    ///
    /// ## Example
    ///
    /// ```rust
    /// use thread_share::EnhancedThreadShare;
    ///
    /// let enhanced = EnhancedThreadShare::new(0);
    /// enhanced.set(100);
    /// assert_eq!(enhanced.get(), 100);
    /// ```
    pub fn set(&self, new_data: T) {
        self.inner.set(new_data);
    }

    pub fn update<F>(&self, f: F)
    where
        F: FnOnce(&mut T),
    {
        self.inner.update(f);
    }

    pub fn read<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&T) -> R,
    {
        self.inner.read(f)
    }

    pub fn write<F, R>(&self, f: F) -> R
    where
        F: FnOnce(&mut T) -> R,
    {
        self.inner.write(f)
    }

    pub fn wait_for_change(&self, timeout: std::time::Duration) -> bool {
        self.inner.wait_for_change(timeout)
    }

    pub fn wait_for_change_forever(&self) {
        self.inner.wait_for_change_forever();
    }

    pub fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            threads: Arc::new(Mutex::new(HashMap::new())),
        }
    }
}

impl<T> Clone for EnhancedThreadShare<T> {
    fn clone(&self) -> Self {
        self.clone()
    }
}

#[cfg(feature = "serialize")]
impl<T: Serialize + Clone + for<'de> Deserialize<'de>> EnhancedThreadShare<T> {
    /// Serializes the shared data to JSON
    ///
    /// This method serializes the current data of type `T` to a JSON string.
    ///
    /// ## Returns
    ///
    /// A JSON string representation of the data.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use thread_share::EnhancedThreadShare;
    ///
    /// let enhanced = EnhancedThreadShare::new(42);
    /// let json_string = enhanced.to_json().expect("Failed to serialize");
    /// assert_eq!(json_string, "42");
    /// ```
    pub fn to_json(&self) -> Result<String, serde_json::Error> {
        serde_json::to_string(&self.inner.get())
    }

    /// Deserializes JSON data back into the shared data
    ///
    /// This method takes a JSON string and attempts to deserialize it back into
    /// the shared data type `T`.
    ///
    /// ## Arguments
    ///
    /// * `json_string` - The JSON string to deserialize
    ///
    /// ## Returns
    ///
    /// `Ok(())` on success, `Err(serde_json::Error)` if deserialization fails.
    ///
    /// ## Example
    ///
    /// ```rust
    /// use thread_share::EnhancedThreadShare;
    ///
    /// let enhanced = EnhancedThreadShare::new(42);
    /// let json_string = enhanced.to_json().unwrap();
    /// enhanced.from_json(&json_string).unwrap();
    /// assert_eq!(enhanced.get(), 42);
    /// ```
    pub fn from_json(&self, json_string: &str) -> Result<(), serde_json::Error> {
        let data: T = serde_json::from_str(json_string)?;
        self.set(data);
        Ok(())
    }
}