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
// Copyright (C) 2024 Ethan Uppal. All rights reserved.
use std::{
    fmt::{Debug, Display},
    hash::{Hash, Hasher},
    sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard}
};

/// A mutable shared pointer.
pub struct MutCell<T> {
    pointer: Arc<RwLock<T>>
}

impl<T> MutCell<T> {
    pub fn new(value: T) -> Self {
        MutCell {
            pointer: Arc::new(RwLock::new(value))
        }
    }

    pub fn as_ref(&self) -> RwLockReadGuard<T> {
        self.pointer.read().unwrap()
    }

    pub fn as_mut(&self) -> RwLockWriteGuard<T> {
        self.pointer.write().unwrap()
    }

    pub fn raw(&self) -> Arc<RwLock<T>> {
        self.pointer.clone()
    }
}

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

impl<T: Clone> MutCell<T> {
    pub fn clone_out(&self) -> T {
        self.as_ref().clone()
    }
}

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

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

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

impl<T: Display> Display for MutCell<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_ref().fmt(f)
    }
}

impl<T: Debug> Debug for MutCell<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_ref().fmt(f)
    }
}