@@ -38,7 +38,7 @@ use std::ffi::CString;
38
38
use std:: mem;
39
39
use std:: ptr;
40
40
41
- use crate :: runtime:: { BOOL , Class , Imp , NO , Object , Protocol , Sel , self } ;
41
+ use crate :: runtime:: { self , Class , Imp , Object , Protocol , Sel , BOOL , NO } ;
42
42
use crate :: { Encode , EncodeArguments , Encoding , Message } ;
43
43
44
44
/// Types that can be used as the implementation of an Objective-C method.
@@ -93,8 +93,7 @@ fn count_args(sel: Sel) -> usize {
93
93
94
94
fn method_type_encoding ( ret : & Encoding , args : & [ Encoding ] ) -> CString {
95
95
// 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 ) ;
98
97
for enc in args {
99
98
use std:: fmt:: Write ;
100
99
write ! ( & mut types, "{}" , enc) . unwrap ( ) ;
@@ -117,13 +116,10 @@ pub struct ClassDecl {
117
116
}
118
117
119
118
impl ClassDecl {
120
- fn with_superclass ( name : & str , superclass : Option < & Class > )
121
- -> Option < ClassDecl > {
119
+ fn with_superclass ( name : & str , superclass : Option < & Class > ) -> Option < ClassDecl > {
122
120
let name = CString :: new ( name) . unwrap ( ) ;
123
121
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 ) } ;
127
123
if cls. is_null ( ) {
128
124
None
129
125
} else {
@@ -150,8 +146,7 @@ impl ClassDecl {
150
146
Functionality it expects, like implementations of `-retain` and `-release`
151
147
used by ARC, will not be present otherwise.
152
148
*/
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 > {
155
150
let mut decl = ClassDecl :: with_superclass ( name, None ) ;
156
151
if let Some ( ref mut decl) = decl {
157
152
unsafe {
@@ -167,17 +162,20 @@ impl ClassDecl {
167
162
/// Unsafe because the caller must ensure that the types match those that
168
163
/// are expected when the method is invoked from Objective-C.
169
164
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
+ {
171
168
let encs = F :: Args :: ENCODINGS ;
172
169
let sel_args = count_args ( sel) ;
173
- assert ! ( sel_args == encs. len( ) ,
170
+ assert ! (
171
+ sel_args == encs. len( ) ,
174
172
"Selector accepts {} arguments, but function accepts {}" ,
175
- sel_args, encs. len( ) ,
173
+ sel_args,
174
+ encs. len( ) ,
176
175
) ;
177
176
178
177
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 ( ) ) ;
181
179
assert ! ( success != NO , "Failed to add method {:?}" , sel) ;
182
180
}
183
181
@@ -187,31 +185,36 @@ impl ClassDecl {
187
185
/// Unsafe because the caller must ensure that the types match those that
188
186
/// are expected when the method is invoked from Objective-C.
189
187
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
+ {
191
191
let encs = F :: Args :: ENCODINGS ;
192
192
let sel_args = count_args ( sel) ;
193
- assert ! ( sel_args == encs. len( ) ,
193
+ assert ! (
194
+ sel_args == encs. len( ) ,
194
195
"Selector accepts {} arguments, but function accepts {}" ,
195
- sel_args, encs. len( ) ,
196
+ sel_args,
197
+ encs. len( ) ,
196
198
) ;
197
199
198
200
let types = method_type_encoding ( & F :: Ret :: ENCODING , encs) ;
199
201
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 ( ) ) ;
202
203
assert ! ( success != NO , "Failed to add class method {:?}" , sel) ;
203
204
}
204
205
205
206
/// Adds an ivar with type `T` and the provided name to self.
206
207
/// 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
+ {
208
212
let c_name = CString :: new ( name) . unwrap ( ) ;
209
213
let encoding = CString :: new ( T :: ENCODING . to_string ( ) ) . unwrap ( ) ;
210
214
let size = mem:: size_of :: < T > ( ) ;
211
215
let align = log2_align_of :: < T > ( ) ;
212
216
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 ( ) )
215
218
} ;
216
219
assert ! ( success != NO , "Failed to add ivar {}" , name) ;
217
220
}
@@ -247,52 +250,66 @@ impl Drop for ClassDecl {
247
250
/// A type for declaring a new protocol and adding new methods to it
248
251
/// before registering it.
249
252
pub struct ProtocolDecl {
250
- proto : * mut Protocol
253
+ proto : * mut Protocol ,
251
254
}
252
255
253
256
impl ProtocolDecl {
254
257
/// Constructs a `ProtocolDecl` with the given name. Returns `None` if the
255
258
/// protocol couldn't be allocated.
256
259
pub fn new ( name : & str ) -> Option < ProtocolDecl > {
257
260
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 ( ) ) } ;
261
262
if proto. is_null ( ) {
262
263
None
263
264
} else {
264
265
Some ( ProtocolDecl { proto : proto } )
265
266
}
266
267
}
267
268
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
+ {
272
278
let encs = Args :: ENCODINGS ;
273
279
let sel_args = count_args ( sel) ;
274
- assert ! ( sel_args == encs. len( ) ,
280
+ assert ! (
281
+ sel_args == encs. len( ) ,
275
282
"Selector accepts {} arguments, but function accepts {}" ,
276
- sel_args, encs. len( ) ,
283
+ sel_args,
284
+ encs. len( ) ,
277
285
) ;
278
286
let types = method_type_encoding ( & Ret :: ENCODING , encs) ;
279
287
unsafe {
280
288
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
+ ) ;
282
295
}
283
296
}
284
297
285
298
/// Adds an instance method declaration with a given description to self.
286
299
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
+ {
289
304
self . add_method_description_common :: < Args , Ret > ( sel, is_required, true )
290
305
}
291
306
292
307
/// Adds a class method declaration with a given description to self.
293
308
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
+ {
296
313
self . add_method_description_common :: < Args , Ret > ( sel, is_required, false )
297
314
}
298
315
0 commit comments