Debian Codegraph @

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

raw
1 use std::fmt;
2 use std::mem;
3 use std::ops::Deref;
4
5 use runtime::{Object, self};
6 use super::WeakPtr;
7
8 /// A pointer that strongly references an object, ensuring it won't be deallocated.
9 pub struct StrongPtr(*mut Object);
10
11 impl StrongPtr {
12 /// Constructs a `StrongPtr` to a newly created object that already has a
13 /// +1 retain count. This will not retain the object.
14 /// When dropped, the object will be released.
15 /// Unsafe because the caller must ensure the given object pointer is valid.
16 pub unsafe fn new(ptr: *mut Object) -> Self {
17 StrongPtr(ptr)
18 }
19
20 /// Retains the given object and constructs a `StrongPtr` to it.
21 /// When dropped, the object will be released.
22 /// Unsafe because the caller must ensure the given object pointer is valid.
23 pub unsafe fn retain(ptr: *mut Object) -> Self {
24 StrongPtr(runtime::objc_retain(ptr))
25 }
26
27 /// Autoreleases self, meaning that the object is not immediately released,
28 /// but will be when the autorelease pool is drained. A pointer to the
29 /// object is returned, but its validity is no longer ensured.
30 pub fn autorelease(self) -> *mut Object {
31 let ptr = self.0;
32 mem::forget(self);
33 unsafe {
34 runtime::objc_autorelease(ptr);
35 }
36 ptr
37 }
38
39 /// Returns a `WeakPtr` to self.
40 pub fn weak(&self) -> WeakPtr {
41 unsafe { WeakPtr::new(self.0) }
42 }
43 }
44
45 impl Drop for StrongPtr {
46 fn drop(&mut self) {
47 unsafe {
48 runtime::objc_release(self.0);
49 }
50 }
51 }
52
53 impl Clone for StrongPtr {
54 fn clone(&self) -> StrongPtr {
55 unsafe {
56 StrongPtr::retain(self.0)
57 }
58 }
59 }
60
61 impl Deref for StrongPtr {
62 type Target = *mut Object;
63
64 fn deref(&self) -> &*mut Object {
65 &self.0
66 }
67 }
68
69 impl fmt::Pointer for StrongPtr {
70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71 fmt::Pointer::fmt(&self.0, f)
72 }
73 }