Debian Codegraph @

third_party/rust/objc/src/rc/weak.rs

raw
1 use std::cell::UnsafeCell;
2 use std::ptr;
3
4 use runtime::{Object, self};
5 use 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.
13 pub struct WeakPtr(Box<UnsafeCell<*mut Object>>);
14
15 impl 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
34 impl Drop for WeakPtr {
35 fn drop(&mut self) {
36 unsafe {
37 runtime::objc_destroyWeak(self.0.get());
38 }
39 }
40 }
41
42 impl 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 }