1use std::fmt;
2use std::mem;
3use std::ops::Deref;
4
5use super::WeakPtr;
6use crate::runtime::{self, Object};
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(unsafe { 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 { StrongPtr::retain(self.0) }
56 }
57}
58
59impl Deref for StrongPtr {
60 type Target = *mut Object;
61
62 fn deref(&self) -> &*mut Object {
63 &self.0
64 }
65}
66
67impl fmt::Pointer for StrongPtr {
68 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 fmt::Pointer::fmt(&self.0, f)
70 }
71}