sovran-arc 0.1.5

Memory management swift-isms brought to Rust
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
use std::fmt::Debug;
use std::sync::{Arc, Mutex, Weak};

/// A wrapper combining Arc and Mutex for convenient shared mutable access to optional values
/// Only works with types that implement Clone
pub struct Arcmo<T: Clone> {
    inner: Arc<Mutex<Option<T>>>,
}

impl<T: Clone> Arcmo<T> {
    /// Creates a new empty Arcmo
    pub fn none() -> Self {
        Self {
            inner: Arc::new(Mutex::new(None)),
        }
    }

    /// Creates a new Arcmo containing Some(value)
    pub fn some(value: T) -> Self {
        Self {
            inner: Arc::new(Mutex::new(Some(value))),
        }
    }

    /// Modifies the contained value using the provided closure.
    /// If no value exists, creates one using T::Default before applying the modification.
    /// Returns the result of the closure.
    pub fn modify<F, R>(&self, f: F) -> R
    where
        T: Default,
        F: FnOnce(&mut T) -> R,
    {
        let mut guard = self
            .inner
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());

        match &mut *guard {
            Some(value) => f(value),
            None => {
                let mut value = T::default();
                let result = f(&mut value);
                *guard = Some(value);
                result
            }
        }
    }

    /// Sets the value to None and returns the previous value if it existed
    pub fn take(&self) -> Option<T> {
        let mut guard = self
            .inner
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        guard.take()
    }

    /// Sets the value to Some(value) and returns the previous value if it existed
    pub fn replace(&self, value: T) -> Option<T> {
        let mut guard = self
            .inner
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        guard.replace(value)
    }

    /// Returns a copy of the contained value if it exists
    pub fn value(&self) -> Option<T> {
        let guard = self
            .inner
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        guard.clone()
    }

    /// Returns true if the contained value is Some
    pub fn is_some(&self) -> bool {
        let guard = self
            .inner
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        guard.is_some()
    }

    /// Returns true if the contained value is None
    pub fn is_none(&self) -> bool {
        let guard = self
            .inner
            .lock()
            .unwrap_or_else(|poisoned| poisoned.into_inner());
        guard.is_none()
    }

    /// Returns a weak reference to the contained value
    pub fn downgrade(&self) -> WeakArcmo<T> {
        WeakArcmo {
            inner: Arc::downgrade(&self.inner),
        }
    }
}

impl<T: Clone> Clone for Arcmo<T> {
    fn clone(&self) -> Self {
        Self {
            inner: Arc::clone(&self.inner),
        }
    }
}

impl<T: Clone + Debug> Debug for Arcmo<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Arcmo").field("inner", &self.inner).finish()
    }
}

impl<T: Clone + Default> Default for Arcmo<T> {
    fn default() -> Self {
        Self::none()
    }
}

/// A weak reference wrapper for Arcmo
pub struct WeakArcmo<T: Clone> {
    inner: Weak<Mutex<Option<T>>>,
}

impl<T: Clone> WeakArcmo<T> {
    /// Attempts to modify the value if it exists and the original Arcmo still exists
    pub fn modify<F, R>(&self, f: F) -> Option<R>
    where
        T: Default,
        F: FnOnce(&mut T) -> R,
    {
        self.inner.upgrade().map(|arc| {
            let mut guard = arc.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            match &mut *guard {
                Some(value) => f(value),
                None => {
                    let mut value = T::default();
                    let result = f(&mut value);
                    *guard = Some(value);
                    result
                }
            }
        })
    }

    /// Attempts to get a copy of the value if it exists and the original Arcmo still exists
    pub fn value(&self) -> Option<T> {
        self.inner.upgrade().and_then(|arc| match arc.lock() {
            Ok(guard) => guard.clone(),
            Err(poisoned) => poisoned.into_inner().clone(),
        })
    }

    /// Returns true if both the original Arcmo exists and contains Some value
    pub fn is_some(&self) -> bool {
        self.inner
            .upgrade()
            .map(|arc| match arc.lock() {
                Ok(guard) => guard.is_some(),
                Err(poisoned) => poisoned.into_inner().is_some(),
            })
            .unwrap_or(false)
    }

    /// Returns true if either the original Arcmo is dropped or contains None
    pub fn is_none(&self) -> bool {
        !self.is_some()
    }

    /// Attempts to replace the value if the original Arcmo still exists
    pub fn replace(&self, value: T) -> Option<Option<T>> {
        self.inner.upgrade().map(|arc| {
            let mut guard = arc.lock().unwrap_or_else(|poisoned| poisoned.into_inner());
            std::mem::replace(&mut *guard, Some(value))
        })
    }
}

impl<T: Clone> Debug for WeakArcmo<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WeakArcmo")
            .field("inner", &self.inner)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::panic::{self, AssertUnwindSafe};
    use std::thread;

    #[derive(Clone, Debug, Default, PartialEq)]
    struct Settings {
        enabled: bool,
        count: i32,
        name: String,
    }

    impl Settings {
        fn update_timestamp(&mut self) {
            self.count += 1;
        }

        fn recalculate_dependencies(&mut self) {
            if self.enabled {
                self.count *= 2;
            }
        }
    }

    #[test]
    fn test_default() {
        let arcmo: Arcmo<Vec<i32>> = Arcmo::default();
        assert!(arcmo.is_none());

        let int_arcmo: Arcmo<i32> = Arcmo::default();
        assert!(int_arcmo.is_none());
    }

    #[test]
    fn test_basic_usage() {
        let v = Arcmo::some(1);
        let result = v.modify(|v| {
            *v = 42;
            *v
        });
        assert_eq!(result, 42);
        assert_eq!(v.value(), Some(42));
    }

    #[test]
    fn test_modification_patterns() {
        // Set whole struct
        let state = Arcmo::none();
        let new_state = Settings {
            enabled: true,
            count: 42,
            name: "test".to_string(),
        };
        let result = state.modify(|s: &mut Settings| {
            *s = new_state.clone();
            s.count
        });
        assert_eq!(result, 42);
        assert_eq!(state.value(), Some(new_state));

        // Update one field
        let counter = Arcmo::<Settings>::none();
        let result = counter.modify(|s: &mut Settings| {
            s.count += 1;
            s.count
        });
        assert_eq!(result, 1);
        assert_eq!(counter.value().unwrap().count, 1);

        // Call methods
        let vec = Arcmo::<Vec<i32>>::none();
        let len = vec.modify(|v: &mut Vec<i32>| {
            v.push(42);
            v.len()
        });
        assert_eq!(len, 1);
        assert_eq!(vec.value(), Some(vec![42]));

        // Complex updates
        let settings = Arcmo::<Settings>::none();
        let count = settings.modify(|s: &mut Settings| {
            s.enabled = true;
            s.update_timestamp();
            s.recalculate_dependencies();
            s.count
        });
        assert_eq!(count, 2); // 1 from timestamp, then doubled
        let result = settings.value().unwrap();
        assert!(result.enabled);
        assert_eq!(result.count, 2);
    }

    #[test]
    fn test_take_and_replace() {
        let v = Arcmo::some(1);
        assert_eq!(v.take(), Some(1));
        assert!(v.is_none());
        assert_eq!(v.replace(42), None);
        assert_eq!(v.value(), Some(42));
    }

    #[test]
    fn test_multiple_references() {
        let v1 = Arcmo::some(1);
        let v2 = v1.clone();

        let result = v1.modify(|v| {
            *v = 42;
            *v
        });
        assert_eq!(result, 42);
        assert_eq!(v2.value(), Some(42));

        v1.take();
        assert!(v2.is_none());
    }

    #[test]
    fn test_modify_none_uses_default() {
        let settings: Arcmo<Settings> = Arcmo::none();

        let count = settings.modify(|s: &mut Settings| {
            assert!(!s.enabled);
            assert_eq!(s.count, 0);
            assert_eq!(s.name, "");
            s.enabled = true;
            s.count
        });

        assert_eq!(count, 0);
        let result = settings.value().unwrap();
        assert!(result.enabled);
        assert_eq!(result.count, 0);
        assert_eq!(result.name, "");
    }

    #[test]
    fn test_weak_reference() {
        let strong = Arcmo::some(42);
        let weak = strong.downgrade();
        assert_eq!(weak.value(), Some(42));
        drop(strong);
        assert_eq!(weak.value(), None);
    }

    #[test]
    fn test_weak_with_none() {
        let strong = Arcmo::none();
        let weak = strong.downgrade();

        assert_eq!(weak.value(), None);
        assert!(weak.is_none());
        assert!(!weak.is_some());

        strong.replace(42);
        assert_eq!(weak.value(), Some(42));
        assert!(!weak.is_none());
        assert!(weak.is_some());

        strong.take();
        assert_eq!(weak.value(), None);
        assert!(weak.is_none());
        assert!(!weak.is_some());
    }

    #[test]
    fn test_weak_modification() {
        let strong = Arcmo::some(vec![1, 2, 3]);
        let weak = strong.downgrade();

        // Modify through weak reference
        let length = weak.modify(|v| {
            v.push(4);
            v.len()
        });
        assert_eq!(length, Some(4));
        assert_eq!(strong.value(), Some(vec![1, 2, 3, 4]));

        // Test with None -> Default
        let strong = Arcmo::<Vec<i32>>::none();
        let weak = strong.downgrade();
        let len = weak.modify(|v| {
            v.push(42);
            v.len()
        });
        assert_eq!(len, Some(1));
        assert_eq!(strong.value(), Some(vec![42]));

        // After dropping the strong reference
        drop(strong);
        let result = weak.modify(|v| {
            v.push(5);
            v.len()
        });
        assert_eq!(result, None);
    }

    #[test]
    fn test_arcmo_poisoned_mutex_recovery() {
        let arcmo = Arcmo::some(42);
        let arcmo_clone = arcmo.clone();

        // Poison the mutex by causing a panic while holding the lock
        let _ = panic::catch_unwind(AssertUnwindSafe(|| {
            let handle = thread::spawn(move || {
                // This will poison the mutex
                arcmo_clone.modify(|_| panic!("Deliberate panic to poison mutex"));
            });

            // Wait for the thread to complete (and panic)
            let _ = handle.join();
        }));

        // Now try to use the poisoned mutex - should recover
        let value = arcmo.value();
        assert_eq!(value, Some(42));

        // Try to modify the poisoned mutex - should recover
        arcmo.modify(|v| *v = 100);
        assert_eq!(arcmo.value(), Some(100));

        // Test other methods with poisoned mutex
        assert!(arcmo.is_some());
        assert!(!arcmo.is_none());

        let taken = arcmo.take();
        assert_eq!(taken, Some(100));
        assert!(arcmo.is_none());

        let replaced = arcmo.replace(200);
        assert_eq!(replaced, None);
        assert_eq!(arcmo.value(), Some(200));
    }

    #[test]
    fn test_weak_arcmo_replace() {
        // Test with Some value
        let strong = Arcmo::some(42);
        let weak = strong.downgrade();

        let prev_value = weak.replace(100);
        assert_eq!(prev_value, Some(Some(42))); // Previous value was Some(42)
        assert_eq!(strong.value(), Some(100)); // New value is 100

        // Test with None value
        let strong_none = Arcmo::<i32>::none();
        let weak_none = strong_none.downgrade();

        let prev_value = weak_none.replace(200);
        assert_eq!(prev_value, Some(None)); // Previous value was None
        assert_eq!(strong_none.value(), Some(200)); // New value is 200

        // Test with dropped strong reference
        drop(strong_none);
        let result = weak_none.replace(300);
        assert_eq!(result, None); // Should return None when strong ref is gone
    }

    #[test]
    fn test_weak_arcmo_poisoned_mutex() {
        let strong = Arcmo::some(42);
        let weak = strong.downgrade();
        let strong_clone = strong.clone();

        // Poison the mutex
        let _ = panic::catch_unwind(AssertUnwindSafe(|| {
            let handle = thread::spawn(move || {
                strong_clone.modify(|_| panic!("Deliberate panic to poison mutex"));
            });
            let _ = handle.join();
        }));

        // Try weak methods with poisoned mutex
        let value = weak.value();
        assert_eq!(value, Some(42));

        assert!(weak.is_some());
        assert!(!weak.is_none());

        let result = weak.modify(|v| {
            *v = 100;
            *v
        });
        assert_eq!(result, Some(100));
        assert_eq!(strong.value(), Some(100));

        // Test replace with poisoned mutex
        let old_value = weak.replace(200);
        assert_eq!(old_value, Some(Some(100)));
        assert_eq!(strong.value(), Some(200));
    }

    #[test]
    fn test_weak_arcmo_none_to_some() {
        // Test upgrading None to Some via replace
        let strong = Arcmo::<i32>::none();
        let weak = strong.downgrade();

        assert!(strong.is_none());
        assert!(weak.is_none());

        // Replace None with Some
        let prev = weak.replace(42);
        assert_eq!(prev, Some(None));
        assert!(strong.is_some());
        assert!(weak.is_some());
        assert_eq!(strong.value(), Some(42));
    }
}