objc_rs/rc/
weak.rs

1use std::cell::UnsafeCell;
2use std::ptr;
3
4use runtime::{Object, self};
5use super::StrongPtr;
6
7// Our pointer must have the same address even if we are moved, so Box it.
8// Although loading the WeakPtr may modify the pointer, it is thread safe,
9// so we must use an UnsafeCell to get a *mut without self being mutable.
10
11/// A pointer that weakly references an object, allowing to safely check
12/// whether it has been deallocated.
13pub struct WeakPtr(Box<UnsafeCell<*mut Object>>);
14
15impl WeakPtr {
16    /// Constructs a `WeakPtr` to the given object.
17    /// Unsafe because the caller must ensure the given object pointer is valid.
18    pub unsafe fn new(obj: *mut Object) -> Self {
19        let ptr = Box::new(UnsafeCell::new(ptr::null_mut()));
20        runtime::objc_initWeak(ptr.get(), obj);
21        WeakPtr(ptr)
22    }
23
24    /// Loads the object self points to, returning a `StrongPtr`.
25    /// If the object has been deallocated, the returned pointer will be null.
26    pub fn load(&self) -> StrongPtr {
27        unsafe {
28            let ptr = runtime::objc_loadWeakRetained(self.0.get());
29            StrongPtr::new(ptr)
30        }
31    }
32}
33
34impl Drop for WeakPtr {
35    fn drop(&mut self) {
36        unsafe {
37            runtime::objc_destroyWeak(self.0.get());
38        }
39    }
40}
41
42impl Clone for WeakPtr {
43    fn clone(&self) -> Self {
44        let ptr = Box::new(UnsafeCell::new(ptr::null_mut()));
45        unsafe {
46            runtime::objc_copyWeak(ptr.get(), self.0.get());
47        }
48        WeakPtr(ptr)
49    }
50}