@@ -38,7 +38,7 @@ use std::ffi::CString;
3838use std:: mem;
3939use std:: ptr;
4040
41- use crate :: runtime:: { BOOL , Class , Imp , NO , Object , Protocol , Sel , self } ;
41+ use crate :: runtime:: { self , Class , Imp , Object , Protocol , Sel , BOOL , NO } ;
4242use crate :: { Encode , EncodeArguments , Encoding , Message } ;
4343
4444/// Types that can be used as the implementation of an Objective-C method.
@@ -93,8 +93,7 @@ fn count_args(sel: Sel) -> usize {
9393
9494fn method_type_encoding ( ret : & Encoding , args : & [ Encoding ] ) -> CString {
9595 // First two arguments are always self and the selector
96- let mut types = format ! ( "{}{}{}" ,
97- ret, <* mut Object >:: ENCODING , Sel :: ENCODING ) ;
96+ let mut types = format ! ( "{}{}{}" , ret, <* mut Object >:: ENCODING , Sel :: ENCODING ) ;
9897 for enc in args {
9998 use std:: fmt:: Write ;
10099 write ! ( & mut types, "{}" , enc) . unwrap ( ) ;
@@ -117,13 +116,10 @@ pub struct ClassDecl {
117116}
118117
119118impl ClassDecl {
120- fn with_superclass ( name : & str , superclass : Option < & Class > )
121- -> Option < ClassDecl > {
119+ fn with_superclass ( name : & str , superclass : Option < & Class > ) -> Option < ClassDecl > {
122120 let name = CString :: new ( name) . unwrap ( ) ;
123121 let super_ptr = superclass. map_or ( ptr:: null ( ) , |c| c) ;
124- let cls = unsafe {
125- runtime:: objc_allocateClassPair ( super_ptr, name. as_ptr ( ) , 0 )
126- } ;
122+ let cls = unsafe { runtime:: objc_allocateClassPair ( super_ptr, name. as_ptr ( ) , 0 ) } ;
127123 if cls. is_null ( ) {
128124 None
129125 } else {
@@ -150,8 +146,7 @@ impl ClassDecl {
150146 Functionality it expects, like implementations of `-retain` and `-release`
151147 used by ARC, will not be present otherwise.
152148 */
153- pub fn root ( name : & str , intitialize_fn : extern fn ( & Class , Sel ) )
154- -> Option < ClassDecl > {
149+ pub fn root ( name : & str , intitialize_fn : extern "C" fn ( & Class , Sel ) ) -> Option < ClassDecl > {
155150 let mut decl = ClassDecl :: with_superclass ( name, None ) ;
156151 if let Some ( ref mut decl) = decl {
157152 unsafe {
@@ -167,17 +162,20 @@ impl ClassDecl {
167162 /// Unsafe because the caller must ensure that the types match those that
168163 /// are expected when the method is invoked from Objective-C.
169164 pub unsafe fn add_method < F > ( & mut self , sel : Sel , func : F )
170- where F : MethodImplementation < Callee =Object > {
165+ where
166+ F : MethodImplementation < Callee = Object > ,
167+ {
171168 let encs = F :: Args :: ENCODINGS ;
172169 let sel_args = count_args ( sel) ;
173- assert ! ( sel_args == encs. len( ) ,
170+ assert ! (
171+ sel_args == encs. len( ) ,
174172 "Selector accepts {} arguments, but function accepts {}" ,
175- sel_args, encs. len( ) ,
173+ sel_args,
174+ encs. len( ) ,
176175 ) ;
177176
178177 let types = method_type_encoding ( & F :: Ret :: ENCODING , encs) ;
179- let success = runtime:: class_addMethod ( self . cls , sel, func. imp ( ) ,
180- types. as_ptr ( ) ) ;
178+ let success = runtime:: class_addMethod ( self . cls , sel, func. imp ( ) , types. as_ptr ( ) ) ;
181179 assert ! ( success != NO , "Failed to add method {:?}" , sel) ;
182180 }
183181
@@ -187,31 +185,36 @@ impl ClassDecl {
187185 /// Unsafe because the caller must ensure that the types match those that
188186 /// are expected when the method is invoked from Objective-C.
189187 pub unsafe fn add_class_method < F > ( & mut self , sel : Sel , func : F )
190- where F : MethodImplementation < Callee =Class > {
188+ where
189+ F : MethodImplementation < Callee = Class > ,
190+ {
191191 let encs = F :: Args :: ENCODINGS ;
192192 let sel_args = count_args ( sel) ;
193- assert ! ( sel_args == encs. len( ) ,
193+ assert ! (
194+ sel_args == encs. len( ) ,
194195 "Selector accepts {} arguments, but function accepts {}" ,
195- sel_args, encs. len( ) ,
196+ sel_args,
197+ encs. len( ) ,
196198 ) ;
197199
198200 let types = method_type_encoding ( & F :: Ret :: ENCODING , encs) ;
199201 let metaclass = ( * self . cls ) . metaclass ( ) as * const _ as * mut _ ;
200- let success = runtime:: class_addMethod ( metaclass, sel, func. imp ( ) ,
201- types. as_ptr ( ) ) ;
202+ let success = runtime:: class_addMethod ( metaclass, sel, func. imp ( ) , types. as_ptr ( ) ) ;
202203 assert ! ( success != NO , "Failed to add class method {:?}" , sel) ;
203204 }
204205
205206 /// Adds an ivar with type `T` and the provided name to self.
206207 /// Panics if the ivar wasn't successfully added.
207- pub fn add_ivar < T > ( & mut self , name : & str ) where T : Encode {
208+ pub fn add_ivar < T > ( & mut self , name : & str )
209+ where
210+ T : Encode ,
211+ {
208212 let c_name = CString :: new ( name) . unwrap ( ) ;
209213 let encoding = CString :: new ( T :: ENCODING . to_string ( ) ) . unwrap ( ) ;
210214 let size = mem:: size_of :: < T > ( ) ;
211215 let align = log2_align_of :: < T > ( ) ;
212216 let success = unsafe {
213- runtime:: class_addIvar ( self . cls , c_name. as_ptr ( ) , size, align,
214- encoding. as_ptr ( ) )
217+ runtime:: class_addIvar ( self . cls , c_name. as_ptr ( ) , size, align, encoding. as_ptr ( ) )
215218 } ;
216219 assert ! ( success != NO , "Failed to add ivar {}" , name) ;
217220 }
@@ -247,52 +250,66 @@ impl Drop for ClassDecl {
247250/// A type for declaring a new protocol and adding new methods to it
248251/// before registering it.
249252pub struct ProtocolDecl {
250- proto : * mut Protocol
253+ proto : * mut Protocol ,
251254}
252255
253256impl ProtocolDecl {
254257 /// Constructs a `ProtocolDecl` with the given name. Returns `None` if the
255258 /// protocol couldn't be allocated.
256259 pub fn new ( name : & str ) -> Option < ProtocolDecl > {
257260 let c_name = CString :: new ( name) . unwrap ( ) ;
258- let proto = unsafe {
259- runtime:: objc_allocateProtocol ( c_name. as_ptr ( ) )
260- } ;
261+ let proto = unsafe { runtime:: objc_allocateProtocol ( c_name. as_ptr ( ) ) } ;
261262 if proto. is_null ( ) {
262263 None
263264 } else {
264265 Some ( ProtocolDecl { proto : proto } )
265266 }
266267 }
267268
268- fn add_method_description_common < Args , Ret > ( & mut self , sel : Sel , is_required : bool ,
269- is_instance_method : bool )
270- where Args : EncodeArguments ,
271- Ret : Encode {
269+ fn add_method_description_common < Args , Ret > (
270+ & mut self ,
271+ sel : Sel ,
272+ is_required : bool ,
273+ is_instance_method : bool ,
274+ ) where
275+ Args : EncodeArguments ,
276+ Ret : Encode ,
277+ {
272278 let encs = Args :: ENCODINGS ;
273279 let sel_args = count_args ( sel) ;
274- assert ! ( sel_args == encs. len( ) ,
280+ assert ! (
281+ sel_args == encs. len( ) ,
275282 "Selector accepts {} arguments, but function accepts {}" ,
276- sel_args, encs. len( ) ,
283+ sel_args,
284+ encs. len( ) ,
277285 ) ;
278286 let types = method_type_encoding ( & Ret :: ENCODING , encs) ;
279287 unsafe {
280288 runtime:: protocol_addMethodDescription (
281- self . proto , sel, types. as_ptr ( ) , is_required as BOOL , is_instance_method as BOOL ) ;
289+ self . proto ,
290+ sel,
291+ types. as_ptr ( ) ,
292+ is_required as BOOL ,
293+ is_instance_method as BOOL ,
294+ ) ;
282295 }
283296 }
284297
285298 /// Adds an instance method declaration with a given description to self.
286299 pub fn add_method_description < Args , Ret > ( & mut self , sel : Sel , is_required : bool )
287- where Args : EncodeArguments ,
288- Ret : Encode {
300+ where
301+ Args : EncodeArguments ,
302+ Ret : Encode ,
303+ {
289304 self . add_method_description_common :: < Args , Ret > ( sel, is_required, true )
290305 }
291306
292307 /// Adds a class method declaration with a given description to self.
293308 pub fn add_class_method_description < Args , Ret > ( & mut self , sel : Sel , is_required : bool )
294- where Args : EncodeArguments ,
295- Ret : Encode {
309+ where
310+ Args : EncodeArguments ,
311+ Ret : Encode ,
312+ {
296313 self . add_method_description_common :: < Args , Ret > ( sel, is_required, false )
297314 }
298315
0 commit comments