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
use crate::marker::{GcDeref, GcDrop, GcSafe};
use crate::{Finalize, Scan, Scanner};

use std::cell::{Cell, RefCell};
use std::sync::{Arc, Mutex, RwLock, TryLockError};

// ARC
unsafe impl<T: ?Sized> GcDeref for Arc<T> where T: GcDeref + Send {}
unsafe impl<T: ?Sized> GcDrop for Arc<T> where T: GcDrop {}
unsafe impl<T: ?Sized> GcSafe for Arc<T> where T: GcSafe {}

// CELL
// unsafe impl<T> !GcDeref for Cell<T> where T: GcDeref {}
unsafe impl<T: ?Sized> GcDrop for Cell<T> where T: GcDrop {}
unsafe impl<T: ?Sized> GcSafe for Cell<T> where T: GcSafe {}

unsafe impl<T: Copy + GcSafe + ?Sized> Scan for Cell<T> {
    #[inline(always)]
    fn scan(&self, _: &mut Scanner<'_>) {
        // A `Copy` type cannot contain a `Gc` so we can make this empty
        // TODO: Document this so we can update this method if this changes
    }
}

unsafe impl<T: Finalize + ?Sized> Finalize for Cell<T> {
    unsafe fn finalize(&mut self) {
        self.get_mut().finalize();
    }
}

// MUTEX
// unsafe impl<T> !GcDeref for Mutex<T> where T: GcDeref {}
unsafe impl<T: ?Sized> GcDrop for Mutex<T> where T: GcDrop {}
unsafe impl<T: ?Sized> GcSafe for Mutex<T> where T: GcSafe {}

unsafe impl<T: Scan + ?Sized> Scan for Mutex<T> {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) {
        match self.try_lock() {
            Ok(data) => {
                let raw: &T = &*data;
                scanner.scan(raw);
            }
            Err(TryLockError::WouldBlock) => {
                error!("A Mutex was in use when it was scanned -- something is buggy here! (no memory unsafety yet, so proceeding...)");
            }
            Err(TryLockError::Poisoned(poison_error)) => {
                let inner_guard = poison_error.into_inner();
                let raw: &T = &*inner_guard;
                scanner.scan(raw);
            }
        }
    }
}

unsafe impl<T: Finalize + ?Sized> Finalize for Mutex<T> {
    unsafe fn finalize(&mut self) {
        let v = self.get_mut();
        match v {
            Ok(v) => v.finalize(),
            Err(e) => e.into_inner().finalize(),
        }
    }
}

// OPTION
unsafe impl<T> GcDeref for Option<T> where T: GcDeref {}
unsafe impl<T> GcDrop for Option<T> where T: GcDrop {}
unsafe impl<T> GcSafe for Option<T> where T: GcSafe {}

unsafe impl<T: Scan> Scan for Option<T> {
    fn scan(&self, scanner: &mut Scanner<'_>) {
        if let Some(v) = self {
            v.scan(scanner);
        }
    }
}

unsafe impl<T: Finalize> Finalize for Option<T> {
    unsafe fn finalize(&mut self) {
        if let Some(v) = self {
            v.finalize();
        }
    }
}

// REFCELL
// unsafe impl<T> !GcDeref for Cell<T> where T: GcDeref {}
unsafe impl<T: ?Sized> GcDrop for RefCell<T> where T: GcDrop {}
unsafe impl<T: ?Sized> GcSafe for RefCell<T> where T: GcSafe {}

unsafe impl<T: Scan + ?Sized> Scan for RefCell<T> {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) {
        // It's an error if this fails
        if let Ok(reference) = self.try_borrow() {
            let raw: &T = &*reference;
            scanner.scan(raw);
        } else {
            error!("A RefCell was in use when it was scanned -- something is buggy here! (no memory unsafety yet, so proceeding...)")
        }
    }
}

unsafe impl<T: Finalize + ?Sized> Finalize for RefCell<T> {
    unsafe fn finalize(&mut self) {
        self.get_mut().finalize();
    }
}

// RESULT
unsafe impl<T, E> GcDeref for Result<T, E>
where
    T: GcDeref,
    E: GcDeref,
{
}
unsafe impl<T, E> GcDrop for Result<T, E>
where
    T: GcDrop,
    E: GcDrop,
{
}
unsafe impl<T, E> GcSafe for Result<T, E>
where
    T: GcSafe,
    E: GcSafe,
{
}

unsafe impl<T: Scan, E: Scan> Scan for Result<T, E> {
    fn scan(&self, scanner: &mut Scanner<'_>) {
        match self {
            Ok(v) => v.scan(scanner),
            Err(e) => e.scan(scanner),
        }
    }
}

unsafe impl<T: Finalize, E: Finalize> Finalize for Result<T, E> {
    unsafe fn finalize(&mut self) {
        match self {
            Ok(v) => v.finalize(),
            Err(e) => e.finalize(),
        }
    }
}

// RWLOCK
// unsafe impl<T> !GcDeref for Mutex<T> where T: GcDeref {}
unsafe impl<T: ?Sized> GcDrop for RwLock<T> where T: GcDrop {}
unsafe impl<T: ?Sized> GcSafe for RwLock<T> where T: GcSafe {}

unsafe impl<T: Scan + ?Sized> Scan for RwLock<T> {
    #[inline]
    fn scan(&self, scanner: &mut Scanner<'_>) {
        match self.try_read() {
            Ok(data) => {
                let raw: &T = &*data;
                scanner.scan(raw);
            }
            Err(TryLockError::WouldBlock) => {
                error!("A RwLock was in use when it was scanned -- something is buggy here! (no memory unsafety yet, so proceeding...)");
            }
            Err(TryLockError::Poisoned(poison_error)) => {
                let inner_guard = poison_error.into_inner();
                let raw: &T = &*inner_guard;
                scanner.scan(raw);
            }
        }
    }
}

unsafe impl<T: Finalize + ?Sized> Finalize for RwLock<T> {
    unsafe fn finalize(&mut self) {
        let v = self.get_mut();
        match v {
            Ok(v) => v.finalize(),
            Err(e) => e.into_inner().finalize(),
        }
    }
}