File tree Expand file tree Collapse file tree 1 file changed +12
-4
lines changed
objc2-foundation/examples Expand file tree Collapse file tree 1 file changed +12
-4
lines changed Original file line number Diff line number Diff line change 1+ #![ deny( unsafe_op_in_unsafe_fn) ]
12use std:: marker:: PhantomData ;
23use 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 }
You can’t perform that action at this time.
0 commit comments