summaryrefslogtreecommitdiff
path: root/matrix-lang/src/value/gc.rs
blob: 5ef8b808aa1eec253ee56c23c125f5487a415703 (plain)
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
use std::{ops::{Index, IndexMut, Deref, DerefMut, Add, Sub, Mul}, marker::PhantomData, ptr::NonNull, fmt::{Debug, Display}};
use crate::prelude::*;

pub struct Gc<T> {
    ptr: NonNull<GcInner<T>>,
    phantom: PhantomData<GcInner<T>>
}

struct GcInner<T> {
    rc: usize,
    data: T
}

impl<T> Gc<T> {
    pub fn new(data: T) -> Self {
        let boxed = Box::new(GcInner {
            rc: 1,
            data,
        });
        Self {
            ptr: NonNull::new(Box::into_raw(boxed)).unwrap(),
            phantom: PhantomData
        }
    }
}

impl <T: Clone> Gc<T> {
    pub fn into_inner(self) -> T {
        unsafe {
            self.ptr.as_ref().data.clone()
        }
    }

    fn data(&self) -> T {
        unsafe {
            self.ptr.as_ref().data.clone()
        }
    }
}

impl <T: ValueClone> ValueClone for Gc<T> {
    fn deep_clone(&self) -> Self {
        unsafe {
            let data = self.ptr.as_ref().data.deep_clone();
            Self::new(data)
        }
    }

    fn shallow_clone(&self) -> Self {
        unsafe {
            let data = self.ptr.as_ref().data.shallow_clone();
            Self::new(data)
        }
    }
}

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

impl<T: IndexMut<Idx>, Idx> IndexMut<Idx> for Gc<T> {
    fn index_mut(&mut self, index: Idx) -> &mut Self::Output {
        self.deref_mut().index_mut(index)
    }
}

impl<T: Index<Idx>, Idx> Index<Idx> for Gc<T> {
    type Output = T::Output;

    fn index(&self, index: Idx) -> &Self::Output {
        self.deref().index(index)
    }
}

impl<T> DerefMut for Gc<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        unsafe {
            &mut self.ptr.as_mut().data
        }
    }
}

impl<T> Deref for Gc<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        unsafe {
            &self.ptr.as_ref().data
        }
    }
}

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

impl <T: Iterator> Iterator for Gc<T> {
    type Item = T::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.deref_mut().next()
    }
}

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

impl<T> Clone for Gc<T> {
    fn clone(&self) -> Self {
        unsafe {
            let inner = self.ptr.as_ptr().as_mut().unwrap();
            inner.rc += 1;

            Self {
                ptr: self.ptr,
                phantom: PhantomData
            }
        }
    }
}

impl<T> Drop for Gc<T> {
    fn drop(&mut self) {
        unsafe {
            let inner = self.ptr.as_mut();
            if inner.rc > 1 {
                inner.rc -= 1;
            } else {
                let _ = Box::from_raw(self.ptr.as_ptr());
            }
        }
    }
}

impl<T: Debug> Debug for Gc<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if f.alternate() {
            write!(f, "{:#?}", self.deref())
        } else {
            write!(f, "{:?}", self.deref())
        }
    }
}

impl<T: Display> Display for Gc<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if f.alternate() {
            write!(f, "{:#}", self.deref())
        } else {
            write!(f, "{}", self.deref())
        }
    }
}

impl <T: Add + Clone> Add for Gc<T> {
    type Output = T::Output;

    fn add(self, rhs: Self) -> Self::Output {
        let a = self.data();
        let b = rhs.data();
        a + b
    }
}

impl <T: Sub + Clone> Sub for Gc<T> {
    type Output = T::Output;

    fn sub(self, rhs: Self) -> Self::Output {
        let a = self.data();
        let b = rhs.data();
        a - b
    }
}

impl <T: Mul + Clone> Mul for Gc<T> {
    type Output = T::Output;

    fn mul(self, rhs: Self) -> Self::Output {
        let a = self.data();
        let b = rhs.data();
        a * b
    }
}