@@ -186,13 +186,13 @@ unsafe impl Send for ClassBuilder {}
186186unsafe impl Sync for ClassBuilder { }
187187
188188impl ClassBuilder {
189- fn as_ptr ( & self ) -> * mut ffi:: objc_class {
189+ fn as_mut_ptr ( & mut self ) -> * mut ffi:: objc_class {
190190 self . cls . as_ptr ( ) . cast ( )
191191 }
192192
193193 fn with_superclass ( name : & str , superclass : Option < & Class > ) -> Option < Self > {
194194 let name = CString :: new ( name) . unwrap ( ) ;
195- let super_ptr = superclass. map_or ( ptr:: null ( ) , |c| c) as _ ;
195+ let super_ptr = superclass. map_or ( ptr:: null ( ) , |c| c) . cast ( ) ;
196196 let cls = unsafe { ffi:: objc_allocateClassPair ( super_ptr, name. as_ptr ( ) , 0 ) } ;
197197 NonNull :: new ( cls. cast ( ) ) . map ( |cls| Self { cls } )
198198 }
@@ -255,15 +255,19 @@ impl ClassBuilder {
255255 let types = method_type_encoding ( & F :: Ret :: ENCODING , encs) ;
256256 let success = Bool :: from_raw ( unsafe {
257257 ffi:: class_addMethod (
258- self . as_ptr ( ) ,
259- sel. as_ptr ( ) as _ ,
258+ self . as_mut_ptr ( ) ,
259+ sel. as_ptr ( ) . cast ( ) ,
260260 Some ( func. __imp ( ) ) ,
261261 types. as_ptr ( ) ,
262262 )
263263 } ) ;
264264 assert ! ( success. as_bool( ) , "Failed to add method {:?}" , sel) ;
265265 }
266266
267+ fn metaclass_mut ( & mut self ) -> * mut ffi:: objc_class {
268+ unsafe { ffi:: object_getClass ( self . as_mut_ptr ( ) . cast ( ) ) as * mut ffi:: objc_class }
269+ }
270+
267271 /// Adds a class method with the given name and implementation.
268272 ///
269273 /// # Panics
@@ -290,11 +294,10 @@ impl ClassBuilder {
290294 ) ;
291295
292296 let types = method_type_encoding ( & F :: Ret :: ENCODING , encs) ;
293- let metaclass = unsafe { self . cls . as_ref ( ) } . metaclass ( ) as * const _ as * mut _ ;
294297 let success = Bool :: from_raw ( unsafe {
295298 ffi:: class_addMethod (
296- metaclass ,
297- sel. as_ptr ( ) as _ ,
299+ self . metaclass_mut ( ) ,
300+ sel. as_ptr ( ) . cast ( ) ,
298301 Some ( func. __imp ( ) ) ,
299302 types. as_ptr ( ) ,
300303 )
@@ -314,7 +317,7 @@ impl ClassBuilder {
314317 let align = log2_align_of :: < T > ( ) ;
315318 let success = Bool :: from_raw ( unsafe {
316319 ffi:: class_addIvar (
317- self . as_ptr ( ) ,
320+ self . as_mut_ptr ( ) ,
318321 c_name. as_ptr ( ) ,
319322 size,
320323 align,
@@ -330,7 +333,7 @@ impl ClassBuilder {
330333 ///
331334 /// If the protocol wasn't successfully added.
332335 pub fn add_protocol ( & mut self , proto : & Protocol ) {
333- let success = unsafe { ffi:: class_addProtocol ( self . as_ptr ( ) , proto. as_ptr ( ) ) } ;
336+ let success = unsafe { ffi:: class_addProtocol ( self . as_mut_ptr ( ) , proto. as_ptr ( ) ) } ;
334337 let success = Bool :: from_raw ( success) . as_bool ( ) ;
335338 assert ! ( success, "Failed to add protocol {:?}" , proto) ;
336339 }
@@ -341,15 +344,15 @@ impl ClassBuilder {
341344 /// to the newly registered [`Class`].
342345 pub fn register ( self ) -> & ' static Class {
343346 // Forget self, otherwise the class will be disposed in drop
344- let cls = ManuallyDrop :: new ( self ) . cls ;
345- unsafe { ffi:: objc_registerClassPair ( cls . as_ptr ( ) . cast ( ) ) } ;
346- unsafe { cls. as_ref ( ) }
347+ let mut this = ManuallyDrop :: new ( self ) ;
348+ unsafe { ffi:: objc_registerClassPair ( this . as_mut_ptr ( ) ) } ;
349+ unsafe { this . cls . as_ref ( ) }
347350 }
348351}
349352
350353impl Drop for ClassBuilder {
351354 fn drop ( & mut self ) {
352- unsafe { ffi:: objc_disposeClassPair ( self . as_ptr ( ) ) }
355+ unsafe { ffi:: objc_disposeClassPair ( self . as_mut_ptr ( ) ) }
353356 }
354357}
355358
@@ -369,7 +372,7 @@ unsafe impl Send for ProtocolBuilder {}
369372unsafe impl Sync for ProtocolBuilder { }
370373
371374impl ProtocolBuilder {
372- fn as_ptr ( & self ) -> * mut ffi:: objc_protocol {
375+ fn as_mut_ptr ( & mut self ) -> * mut ffi:: objc_protocol {
373376 self . proto . as_ptr ( ) . cast ( )
374377 }
375378
@@ -378,8 +381,8 @@ impl ProtocolBuilder {
378381 /// Returns [`None`] if the protocol couldn't be allocated.
379382 pub fn new ( name : & str ) -> Option < Self > {
380383 let c_name = CString :: new ( name) . unwrap ( ) ;
381- let proto = unsafe { ffi:: objc_allocateProtocol ( c_name. as_ptr ( ) ) } as * mut Protocol ;
382- NonNull :: new ( proto) . map ( |proto| Self { proto } )
384+ let proto = unsafe { ffi:: objc_allocateProtocol ( c_name. as_ptr ( ) ) } ;
385+ NonNull :: new ( proto. cast ( ) ) . map ( |proto| Self { proto } )
383386 }
384387
385388 fn add_method_description_common < Args , Ret > (
@@ -403,8 +406,8 @@ impl ProtocolBuilder {
403406 let types = method_type_encoding ( & Ret :: ENCODING , encs) ;
404407 unsafe {
405408 ffi:: protocol_addMethodDescription (
406- self . as_ptr ( ) ,
407- sel. as_ptr ( ) as _ ,
409+ self . as_mut_ptr ( ) ,
410+ sel. as_ptr ( ) . cast ( ) ,
408411 types. as_ptr ( ) ,
409412 Bool :: new ( is_required) . as_raw ( ) ,
410413 Bool :: new ( is_instance_method) . as_raw ( ) ,
@@ -433,15 +436,15 @@ impl ProtocolBuilder {
433436 /// Adds a requirement on another protocol.
434437 pub fn add_protocol ( & mut self , proto : & Protocol ) {
435438 unsafe {
436- ffi:: protocol_addProtocol ( self . as_ptr ( ) , proto. as_ptr ( ) ) ;
439+ ffi:: protocol_addProtocol ( self . as_mut_ptr ( ) , proto. as_ptr ( ) ) ;
437440 }
438441 }
439442
440443 /// Registers the [`ProtocolBuilder`], consuming it and returning a reference
441444 /// to the newly registered [`Protocol`].
442- pub fn register ( self ) -> & ' static Protocol {
445+ pub fn register ( mut self ) -> & ' static Protocol {
443446 unsafe {
444- ffi:: objc_registerProtocol ( self . as_ptr ( ) ) ;
447+ ffi:: objc_registerProtocol ( self . as_mut_ptr ( ) ) ;
445448 self . proto . as_ref ( )
446449 }
447450 }
0 commit comments