sync-cell 0.2.0

A collection of easier to use thread-safe types for the creation of larger thread safe systems.
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
//! A module containing easier to use thread-safe types for the creation of larger thread safe
//! systems.
//!
//! ## Included Types
//! - `SyncCell` - A replacement for `std::cell::RefCell` and `std::cell::Cell` with an easier to
//! use API than `std::sync::RwLock`.
//! - `HeldSyncCell` - A cell that maintains a previous value until the `update` method is called
//! at which point any changes to the value are applied.

use std::{sync::{RwLock, RwLockReadGuard, RwLockWriteGuard}, cmp::Ordering, hash::{Hash, Hasher}, mem::swap};

/// A mutable memory location that can be modified safely from multiple threads.
/// This structure is similar to `std::cell::Cell` or `std::cell::RefCell`
/// while being thread-safe.
/// It functions as a thin wrapper around `std::sync::RwLock` while assuming that poisoned locks
/// indicate an unrecoverable error. This makes it more ergonomic to use than `RwLock` at the cost
/// of some stability.
/// 
/// # As a `Cell` replacement.
/// `SyncCell` can be used to replace the functionality of a `std::cell::Cell` in contexts where
/// data need to mutably accessed across multiple threads.
/// ## Using `std::cell::Cell`
/// ```
/// use std::cell::Cell;
///
/// let cell = Cell::new(0);
///
/// cell.set(1);
///
/// println!("{}", cell.get());
/// ```
/// ## Using `sync_cell::SyncCell`
/// ```
/// use sync_cell::SyncCell;
///
/// let cell = SyncCell::new(0);
///
/// cell.set(1);
///
/// println!("{}", cell.get());
/// ```
///
/// # As a `RefCell` replacement.
/// `SyncCell` can also be used to replace usages of `RefCell`.
/// ## Using `std::cell::RefCell`
/// ```
/// use std::cell::RefCell;
///
/// let cell = RefCell::new((0, 1));
///
/// let borrowed = cell.borrow();
/// println!("{}", borrowed.0);
/// drop(borrowed);
///
/// let mut mutable_borrow = cell.borrow_mut();
/// mutable_borrow.1 = 2;
/// drop(mutable_borrow);
///
/// let borrowed = cell.borrow();
/// println!("{:?}", borrowed);
/// ```
/// ## Using `sync_cell::SyncCell`
/// ```
/// use sync_cell::SyncCell;
///
/// let cell = SyncCell::new((0, 1));
///
/// let borrowed = cell.borrow();
/// println!("{}", borrowed.0);
/// drop(borrowed);
///
/// let mut mutable_borrow = cell.borrow_mut();
/// mutable_borrow.1 = 2;
/// drop(mutable_borrow);
///
/// let borrowed = cell.borrow();
/// println!("{:?}", borrowed);
/// ```
///
/// # Panicking
/// Unlike `std::sync::RwLock`, `SyncCell` will panic rather than return an error when the lock
/// becomes poisoned.
#[derive(Debug)]
pub struct SyncCell<T: ?Sized> {
    /// The internal lock holding the data of this cell.
    data: RwLock<T>,
}

impl <T> SyncCell<T> {
    /// Creates a new `SyncCell`.
    ///
    /// - `data` - The initial value of the `SyncCell`.
    pub const fn new(data: T) -> Self {
        Self {
            data: RwLock::new(data)
        }
    }

    /// Sets the value contained in this cell.
    ///
    /// - `value` - The new value of the cell.
    ///
    /// # Panicking
    /// This method will panic if the lock becomes poisoned.
    pub fn set(&self, value: T) {
        match self.data.write() {
            Ok(mut data) => *data = value,
            Err(err) => panic!("Failed to set cell value. Lock was poisoned: {}", err),
        }
    }

    /// Retrieves the inner value stored in this `SyncCell`. 
    ///
    /// # Panicking
    /// This method will panic if the lock becomes poisoned.
    pub fn into_inner(self) -> T {
        match self.data.into_inner() {
            Ok(data) => data,
            Err(err) => panic!("Failed to get cell value. Lock was poisoned: {}", err),
        }
    }

    /// Replaces the internal value contained in this cell.
    /// The previous value is returned.
    ///
    /// - `value` - The new value of the cell.
    ///
    /// # Panicking
    /// This method will panic if the lock becomes poisoned.
    pub fn replace(&self, mut value: T) -> T {
        match self.data.write() {
            Ok(mut data) => {
                swap(&mut *data, &mut value);
                value
            },
            Err(err) => panic!("Failed to set cell value. Lock was poisoned: {}", err),
        }
    }
}

impl <T: ?Sized> SyncCell<T> {
    /// Borrows a immutable reference to the data stored in this cell.
    ///
    /// # Panicking
    /// This method will panic if the lock becomes poisoned.
    pub fn borrow(&self) -> RwLockReadGuard<T> {
        match self.data.read() {
            Ok(data) => data,
            Err(err) => panic!("Failed to get cell value. Lock was poisoned: {}", err),
        }
    }
    
    /// Borrows a mutable reference to the data stored in this cell.
    ///
    /// # Panicking
    /// This method will panic if the lock becomes poisoned.
    pub fn borrow_mut(&self) -> RwLockWriteGuard<T> {
        match self.data.write() {
            Ok(data) => data,
            Err(err) => panic!("Failed to get cell value. Lock was poisoned: {}", err),
        }
    }
}

impl <T: Clone> SyncCell<T> {
    /// Gets the value contained in this cell.
    ///
    /// # Panicking
    /// This method will panic if the lock becomes poisoned.
    pub fn get(&self) -> T {
        match self.data.read() {
            Ok(data) => data.clone(),
            Err(err) => panic!("Failed to get cell value. Lock was poisoned: {}", err),
        }
    }
}

impl <T: Clone> Clone for SyncCell<T> {
    fn clone(&self) -> Self {
        Self::new(self.get())
    }
}

impl <T: Default> Default for SyncCell<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl <T: PartialEq + ?Sized> PartialEq for SyncCell<T> {
    fn eq(&self, other: &Self) -> bool {
        self.borrow().eq(&*other.borrow())
    }
}

impl <T: Eq + ?Sized> Eq for SyncCell<T> {
}

impl <T: PartialOrd + ?Sized> PartialOrd for SyncCell<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.borrow().partial_cmp(&*other.borrow())
    }
}

impl <T: Ord + ?Sized> Ord for SyncCell<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.borrow().cmp(&*other.borrow())
    }
}

impl <T: Hash + ?Sized> Hash for SyncCell<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.borrow().hash(state)
    }
}

impl <T> From<T> for SyncCell<T> {
    fn from(value: T) -> Self {
        Self::new(value)
    }
}

/// A cell that holds a value until any changes made are applied by use of the `update` method.
/// Getting the value or obtaining a reference to the value in this cell will return the value
/// immediately following the last call to `update`. This allows for mutably altering a value while
/// keeping a reference for a short amount of time to the old value.
/// This is useful when you want a method in a structure to be able to modify the structure it is
/// being called from such as when changing the scene in a game engine.
///
/// # Usage
/// ```
/// use sync_cell::HeldSyncCell;
///
/// let cell = HeldSyncCell::new(0);
///
/// // Set the next value of the cell.
/// cell.set(1);
///
/// // Cell continues to hold a value of 0 until the `update` method is called.
/// assert_eq!(0, cell.get());
///
/// cell.update();
/// assert_eq!(1, cell.get());
/// ```
pub struct HeldSyncCell<T> {
    /// The current value that is made available.
    current_value: SyncCell<T>,
    /// The value to use next.
    next_value: SyncCell<Option<T>>,
}

impl <T> HeldSyncCell<T> {
    /// Creates a new `HeldSyncCell`.
    ///
    /// - `data` - The initial value of the `HeldSyncCell`.
    pub const fn new(data: T) -> Self {
        Self {
            current_value: SyncCell::new(data),
            next_value: SyncCell::new(None),
        }
    }

    /// Sets the value contained in this cell.
    /// This value will only become available once the `update` method is called.
    /// 
    /// In the case that multiple threads call this method simultaniously,
    /// the order in which the calls are processed is not defined. However, the final result will
    /// be the value specified by one of the method calls.
    ///
    /// - `value` - The new value of the cell.
    ///
    /// # Panicking
    /// This method will panic if any of the locks become poisoned.
    pub fn set(&self, value: T) {
        self.next_value.set(Some(value))
    }

    /// Retrieves the inner value stored in this `HeldSyncCell`. 
    /// This will return the most up-to-date value even if `update` has not been called.
    ///
    /// # Panicking
    /// This method will panic if any of the locks become poisoned.
    pub fn into_inner(self) -> T {
        self.next_value.into_inner()
            .unwrap_or(self.current_value.into_inner())
    }

    /// Borrows a immutable reference to the data stored in this cell.
    /// This is a reference to the current value of the cell.
    ///
    /// # Panicking
    /// This method will panic if any of the locks become poisoned.
    pub fn borrow(&self) -> RwLockReadGuard<T> {
        self.current_value.borrow()
    }
    
    /// Borrows a mutable reference to the data stored in this cell.
    /// This is a reference to the current value of the cell not the incoming value. Any changes to
    /// the value will update the current value.
    ///
    /// # Panicking
    /// This method will panic if any of the locks become poisoned.
    pub fn borrow_mut(&self) -> RwLockWriteGuard<T> {
        self.current_value.borrow_mut()
    }

    /// Checks if a new nalue is available that can be applied by calling `update`.
    ///
    /// # Panicking
    /// This method will panic if any of the locks become poisoned.
    pub fn has_update(&self) -> bool {
        self.next_value.borrow().is_some()
    }

    /// Updates the internal value of this cell.
    /// This involves replacing the current value with the incoming value if it is available.
    ///
    /// # Panicking
    /// This method will panic if any of the locks become poisoned.
    pub fn update(&self) {
        if let Some(next) = self.next_value.replace(None) {
            self.current_value.set(next);
        }
    } 
}

impl <T: Clone> HeldSyncCell<T> {
    /// Gets the value contained in this cell.
    ///
    /// # Panicking
    /// This method will panic if any of the locks become poisoned.
    pub fn get(&self) -> T {
        self.current_value.get()
    }
}

impl <T: Clone> Clone for HeldSyncCell<T> {
    fn clone(&self) -> Self {
        Self::new(self.get())
    }
}

impl <T: Default> Default for HeldSyncCell<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl <T: PartialEq> PartialEq for HeldSyncCell<T> {
    fn eq(&self, other: &Self) -> bool {
        self.borrow().eq(&*other.borrow())
    }
}

impl <T: Eq> Eq for HeldSyncCell<T> {
}

impl <T: PartialOrd> PartialOrd for HeldSyncCell<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.borrow().partial_cmp(&*other.borrow())
    }
}

impl <T: Ord> Ord for HeldSyncCell<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.borrow().cmp(&*other.borrow())
    }
}

impl <T: Hash> Hash for HeldSyncCell<T> {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.borrow().hash(state)
    }
}

impl <T> From<T> for HeldSyncCell<T> {
    fn from(value: T) -> Self {
        Self::new(value)
    }
}

#[cfg(test)]
mod tests {
    use core::panic;
    use std::{thread, sync::Arc};

    use crate::{SyncCell, HeldSyncCell};

    #[test]
    pub fn test_sync_cell_new() {
        let _cell = SyncCell::new(1);
    } 

    #[test]
    pub fn test_sync_cell_set() {
        let cell = SyncCell::new(2);

        cell.set(3);

        assert_eq!(3, cell.get())
    }
    
    #[test]
    pub fn test_sync_cell_get() {
        let cell = SyncCell::new(4);

        assert_eq!(4, cell.get())
    }
    
    #[test]
    pub fn test_sync_cell_replace() {
        let cell = SyncCell::new(2);

        let old = cell.replace(3);
        
        assert_eq!(2, old);
        assert_eq!(3, cell.get())
    }
    
    #[test]
    #[should_panic]
    pub fn test_sync_cell_replace_poisoned() {
        let cell = Arc::new(SyncCell::new(4));

        let cell2 = cell.clone();

        let _ = thread::spawn(move || {
            let _borrow = cell2.borrow();

            panic!("Intentional panic.");
        }).join();

        let old = cell.replace(3);
        
        assert_ne!(2, old);
        assert_ne!(3, cell.get())
    }
    
    #[test]
    pub fn test_sync_cell_into_inner() {
        let cell = SyncCell::new(4);

        assert_eq!(4, cell.into_inner())
    }
    
    #[test]
    pub fn test_sync_cell_mutable_borrow() {
        let cell = SyncCell::new(4);

        let mut borrow = cell.borrow_mut();

        *borrow = 5;

        drop(borrow);

        assert_eq!(5, cell.get())
    }
    
    #[test]
    #[should_panic]
    pub fn test_sync_cell_mutable_borrow_poisoned() {
        let cell = Arc::new(SyncCell::new(4));

        let cell2 = cell.clone();

        let _ = thread::spawn(move || {
            let _borrow = cell2.borrow();

            panic!("Intentional panic.");
        }).join();

        let mut borrow = cell.borrow_mut();

        *borrow = 5;

        drop(borrow);

        assert_ne!(5, cell.get())
    }
    
    #[test]
    #[should_panic]
    pub fn test_sync_cell_get_poisoned() {
        let cell = Arc::new(SyncCell::new(4));

        let cell2 = cell.clone();

        let _ = thread::spawn(move || {
            let _borrow = cell2.borrow();

            panic!("Intentional panic.");
        }).join();

        assert_ne!(4, cell.get())
    }
    
    #[test]
    #[should_panic]
    pub fn test_sync_cell_set_poisoned() {
        let cell = Arc::new(SyncCell::new(4));

        let cell2 = cell.clone();

        let _ = thread::spawn(move || {
            let _borrow = cell2.borrow();

            panic!("Intentional panic.");
        }).join();

        cell.set(5);

        assert_ne!(5, cell.get());
    }

    #[test]
    pub fn test_held_sync_cell_new() {
        let _cell = HeldSyncCell::new(0);
    }
    
    #[test]
    pub fn test_held_sync_cell_get() {
        let cell = HeldSyncCell::new(1);

        assert_eq!(1, cell.get())
    }
    
    #[test]
    pub fn test_held_sync_cell_set_no_update() {
        let cell = HeldSyncCell::new(1);

        cell.set(2);

        assert_eq!(true, cell.has_update());
        assert_eq!(1, cell.get())
    }
    
    #[test]
    pub fn test_held_sync_cell_set_update() {
        let cell = HeldSyncCell::new(1);

        cell.set(2);
        cell.update();

        assert_eq!(false, cell.has_update());
        assert_eq!(2, cell.get())
    }
    
    #[test]
    pub fn test_held_sync_cell_set_double_update() {
        let cell = HeldSyncCell::new(1);

        cell.set(2);
        cell.update();
        cell.update();

        assert_eq!(false, cell.has_update());
        assert_eq!(2, cell.get())
    }

    #[test]
    pub fn test_held_sync_cell_no_set_update() {
        let cell = HeldSyncCell::new(1);

        cell.update();

        assert_eq!(false, cell.has_update());
        assert_eq!(1, cell.get())
    }

    #[test]
    pub fn test_held_sync_cell_no_set() {
        let cell = HeldSyncCell::new(1);

        assert_eq!(false, cell.has_update());
        assert_eq!(1, cell.get())
    }
    
    #[test]
    pub fn test_held_sync_cell_into_inner() {
        let cell = HeldSyncCell::new(4);

        assert_eq!(4, cell.into_inner())
    }
    
    #[test]
    pub fn test_held_sync_cell_set_into_inner() {
        let cell = HeldSyncCell::new(4);

        cell.set(5);

        assert_eq!(5, cell.into_inner())
    }
    
    #[test]
    pub fn test_held_sync_cell_set_update_into_inner() {
        let cell = HeldSyncCell::new(4);

        cell.set(5);
        cell.update();

        assert_eq!(5, cell.into_inner())
    }
    
    #[test]
    pub fn test_held_sync_cell_mutable_borrow() {
        let cell = HeldSyncCell::new(4);

        let mut borrow = cell.borrow_mut();

        *borrow = 5;

        drop(borrow);

        assert_eq!(5, cell.get())
    }
    
    #[test]
    pub fn test_held_sync_cell_mutable_borrow_set() {
        let cell = HeldSyncCell::new(4);

        let mut borrow = cell.borrow_mut();

        *borrow = 5;

        cell.set(6);

        drop(borrow);

        assert_eq!(5, cell.get());
        cell.update();
        assert_eq!(6, cell.get());
    }
}