makepad_objc_sys/rc/
strong.rs1use std::fmt;
2use std::mem;
3use std::ops::Deref;
4
5use runtime::{Object, self};
6use super::WeakPtr;
7
8pub struct StrongPtr(*mut Object);
10
11impl StrongPtr {
12 pub unsafe fn new(ptr: *mut Object) -> Self {
17 StrongPtr(ptr)
18 }
19
20 pub unsafe fn retain(ptr: *mut Object) -> Self {
24 StrongPtr(runtime::objc_retain(ptr))
25 }
26
27 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 pub fn weak(&self) -> WeakPtr {
41 unsafe { WeakPtr::new(self.0) }
42 }
43}
44
45impl Drop for StrongPtr {
46 fn drop(&mut self) {
47 unsafe {
48 runtime::objc_release(self.0);
49 }
50 }
51}
52
53impl Clone for StrongPtr {
54 fn clone(&self) -> StrongPtr {
55 unsafe {
56 StrongPtr::retain(self.0)
57 }
58 }
59}
60
61impl Deref for StrongPtr {
62 type Target = *mut Object;
63
64 fn deref(&self) -> &*mut Object {
65 &self.0
66 }
67}
68
69impl fmt::Pointer for StrongPtr {
70 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71 fmt::Pointer::fmt(&self.0, f)
72 }
73}