// Our pointer must have the same address even if we are moved, so Box it. // Although loading the WeakPtr may modify the pointer, it is thread safe, // so we must use an UnsafeCell to get a *mut without self being mutable.
/// A pointer that weakly references an object, allowing to safely check /// whether it has been deallocated. pubstruct WeakPtr(Box<UnsafeCell<*mut Object>>);
impl WeakPtr { /// Constructs a `WeakPtr` to the given object. /// Unsafe because the caller must ensure the given object pointer is valid. pubunsafefn new(obj: *mut Object) -> Self { let ptr = Box::new(UnsafeCell::new(ptr::null_mut()));
runtime::objc_initWeak(ptr.get(), obj);
WeakPtr(ptr)
}
/// Loads the object self points to, returning a `StrongPtr`. /// If the object has been deallocated, the returned pointer will be null. pubfn load(&self) -> StrongPtr { unsafe { let ptr = runtime::objc_loadWeakRetained(self.0.get());
StrongPtr::new(ptr)
}
}
}
impl Drop for WeakPtr { fn drop(&mutself) { unsafe {
runtime::objc_destroyWeak(self.0.get());
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.