pub struct StrongPtr(/* private fields */);
Expand description
A pointer that strongly references an object, ensuring it won’t be deallocated.
Implementations§
Source§impl StrongPtr
impl StrongPtr
Sourcepub unsafe fn new(ptr: *mut Object) -> Self
pub unsafe fn new(ptr: *mut Object) -> Self
Constructs a StrongPtr
to a newly created object that already has a
+1 retain count. This will not retain the object.
When dropped, the object will be released.
Unsafe because the caller must ensure the given object pointer is valid.
Examples found in repository?
examples/example.rs (line 23)
8fn main() {
9 // Get a class
10 let cls = class!(NSObject);
11 println!("NSObject size: {}", cls.instance_size());
12
13 // Inspect its ivars
14 println!("NSObject ivars:");
15 for ivar in cls.instance_variables().iter() {
16 println!("{}", ivar.name());
17 }
18
19 // Allocate an instance
20 let obj = unsafe {
21 let obj: *mut Object = msg_send![cls, alloc];
22 let obj: *mut Object = msg_send![obj, init];
23 StrongPtr::new(obj)
24 };
25 println!("NSObject address: {:p}", obj);
26
27 // Access an ivar of the object
28 let isa: *const Class = unsafe {
29 *(**obj).get_ivar("isa")
30 };
31 println!("NSObject isa: {:?}", isa);
32
33 // Inspect a method of the class
34 let hash_sel = sel!(hash);
35 let hash_method = cls.instance_method(hash_sel).unwrap();
36 let hash_return = hash_method.return_type();
37 println!("-[NSObject hash] return type: {:?}", hash_return);
38 assert!(hash_return == usize::encode());
39
40 // Invoke a method on the object
41 let hash: usize = unsafe {
42 msg_send![*obj, hash]
43 };
44 println!("NSObject hash: {}", hash);
45}
Sourcepub unsafe fn retain(ptr: *mut Object) -> Self
pub unsafe fn retain(ptr: *mut Object) -> Self
Retains the given object and constructs a StrongPtr
to it.
When dropped, the object will be released.
Unsafe because the caller must ensure the given object pointer is valid.
Sourcepub fn autorelease(self) -> *mut Object
pub fn autorelease(self) -> *mut Object
Autoreleases self, meaning that the object is not immediately released, but will be when the autorelease pool is drained. A pointer to the object is returned, but its validity is no longer ensured.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for StrongPtr
impl RefUnwindSafe for StrongPtr
impl !Send for StrongPtr
impl !Sync for StrongPtr
impl Unpin for StrongPtr
impl UnwindSafe for StrongPtr
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more