Skip to content

Commit 52ff35d

Browse files
committed
Fix initWithPtr: method in class_with_lifetime example
1 parent 61ac4f1 commit 52ff35d

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

objc2-foundation/examples/class_with_lifetime.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
12
use std::marker::PhantomData;
23
use std::sync::Once;
34

@@ -50,15 +51,22 @@ impl<'a> MyObject<'a> {
5051
let mut decl = ClassDecl::new("MyObject", superclass).unwrap();
5152
decl.add_ivar::<Option<&mut u8>>("_number_ptr");
5253

53-
extern "C" fn init_with_ptr(this: &mut Object, _cmd: Sel, ptr: *mut u8) -> *mut Object {
54-
unsafe {
55-
this.set_ivar("_number_ptr", ptr);
54+
unsafe extern "C" fn init_with_ptr(
55+
this: *mut Object,
56+
_cmd: Sel,
57+
ptr: *mut u8,
58+
) -> *mut Object {
59+
let this: *mut Object = unsafe { msg_send![super(this, NSObject::class()), init] };
60+
if let Some(this) = unsafe { this.as_mut() } {
61+
unsafe {
62+
this.set_ivar("_number_ptr", ptr);
63+
}
5664
}
5765
this
5866
}
5967

6068
unsafe {
61-
let init_with_ptr: extern "C" fn(&mut Object, Sel, *mut u8) -> *mut Object =
69+
let init_with_ptr: unsafe extern "C" fn(*mut Object, Sel, *mut u8) -> *mut Object =
6270
init_with_ptr;
6371
decl.add_method(sel!(initWithPtr:), init_with_ptr);
6472
}

0 commit comments

Comments
 (0)