Skip to main content

objc_rs/rc/
weak.rs

1use std::cell::UnsafeCell;
2use std::ptr;
3
4use super::StrongPtr;
5use crate::runtime::{self, Object};
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        unsafe {
21            runtime::objc_initWeak(ptr.get(), obj);
22        }
23        WeakPtr(ptr)
24    }
25
26    /// Loads the object self points to, returning a `StrongPtr`.
27    /// If the object has been deallocated, the returned pointer will be null.
28    pub fn load(&self) -> StrongPtr {
29        unsafe {
30            let ptr = runtime::objc_loadWeakRetained(self.0.get());
31            StrongPtr::new(ptr)
32        }
33    }
34}
35
36impl Drop for WeakPtr {
37    fn drop(&mut self) {
38        unsafe {
39            runtime::objc_destroyWeak(self.0.get());
40        }
41    }
42}
43
44impl Clone for WeakPtr {
45    fn clone(&self) -> Self {
46        let ptr = Box::new(UnsafeCell::new(ptr::null_mut()));
47        unsafe {
48            runtime::objc_copyWeak(ptr.get(), self.0.get());
49        }
50        WeakPtr(ptr)
51    }
52}