1
1
/*!
2
2
Functionality for declaring Objective-C classes.
3
3
4
- Classes can be declared using the `ClassDecl` struct. Instance variables and
4
+ Classes can be declared using the [ `ClassDecl`] struct. Instance variables and
5
5
methods can then be added before the class is ultimately registered.
6
6
7
7
# Example
@@ -13,7 +13,6 @@ one ivar, a `u32` named `_number` and a `number` method that returns it:
13
13
# use objc::{class, sel};
14
14
# use objc::declare::ClassDecl;
15
15
# use objc::runtime::{Class, Object, Sel};
16
- # fn main() {
17
16
let superclass = class!(NSObject);
18
17
let mut decl = ClassDecl::new("MyNumber", superclass).unwrap();
19
18
@@ -30,7 +29,6 @@ unsafe {
30
29
}
31
30
32
31
decl.register();
33
- # }
34
32
```
35
33
*/
36
34
@@ -52,7 +50,7 @@ pub trait MethodImplementation {
52
50
/// The argument types of the method.
53
51
type Args : EncodeArguments ;
54
52
55
- /// Returns self as an `Imp` of a method.
53
+ /// Returns self as an [ `Imp`] of a method.
56
54
fn imp ( self ) -> Imp ;
57
55
}
58
56
@@ -129,20 +127,21 @@ impl ClassDecl {
129
127
}
130
128
}
131
129
132
- /// Constructs a `ClassDecl` with the given name and superclass.
133
- /// Returns `None` if the class couldn't be allocated.
130
+ /// Constructs a [`ClassDecl`] with the given name and superclass.
131
+ ///
132
+ /// Returns [`None`] if the class couldn't be allocated.
134
133
pub fn new ( name : & str , superclass : & Class ) -> Option < ClassDecl > {
135
134
ClassDecl :: with_superclass ( name, Some ( superclass) )
136
135
}
137
136
138
137
/**
139
- Constructs a `ClassDecl` declaring a new root class with the given name.
140
- Returns `None` if the class couldn't be allocated.
138
+ Constructs a [ `ClassDecl`] declaring a new root class with the given name.
139
+ Returns [ `None`] if the class couldn't be allocated.
141
140
142
141
An implementation for `+initialize` must also be given; the runtime calls
143
142
this method for all classes, so it must be defined on root classes.
144
143
145
- Note that implementing a root class is not a simple endeavor.
144
+ Note that implementing a root class is not a simple endeavor!
146
145
For example, your class probably cannot be passed to Cocoa code unless
147
146
the entire `NSObject` protocol is implemented.
148
147
Functionality it expects, like implementations of `-retain` and `-release`
@@ -158,9 +157,12 @@ impl ClassDecl {
158
157
decl
159
158
}
160
159
161
- /// Adds a method with the given name and implementation to self.
162
- /// Panics if the method wasn't sucessfully added
163
- /// or if the selector and function take different numbers of arguments.
160
+ /// Adds a method with the given name and implementation.
161
+ ///
162
+ /// # Panics
163
+ ///
164
+ /// Panics if the method wasn't sucessfully added or if the selector and
165
+ /// function take different numbers of arguments.
164
166
///
165
167
/// # Safety
166
168
///
@@ -184,9 +186,12 @@ impl ClassDecl {
184
186
assert ! ( success != NO , "Failed to add method {:?}" , sel) ;
185
187
}
186
188
187
- /// Adds a class method with the given name and implementation to self.
188
- /// Panics if the method wasn't sucessfully added
189
- /// or if the selector and function take different numbers of arguments.
189
+ /// Adds a class method with the given name and implementation.
190
+ ///
191
+ /// # Panics
192
+ ///
193
+ /// Panics if the method wasn't sucessfully added or if the selector and
194
+ /// function take different numbers of arguments.
190
195
///
191
196
/// # Safety
192
197
///
@@ -211,12 +216,12 @@ impl ClassDecl {
211
216
assert ! ( success != NO , "Failed to add class method {:?}" , sel) ;
212
217
}
213
218
214
- /// Adds an ivar with type `T` and the provided name to self .
215
- /// Panics if the ivar wasn't successfully added.
216
- pub fn add_ivar < T > ( & mut self , name : & str )
217
- where
218
- T : Encode ,
219
- {
219
+ /// Adds an ivar with type `T` and the provided name.
220
+ ///
221
+ /// # Panics
222
+ ///
223
+ /// If the ivar wasn't successfully added.
224
+ pub fn add_ivar < T : Encode > ( & mut self , name : & str ) {
220
225
let c_name = CString :: new ( name) . unwrap ( ) ;
221
226
let encoding = CString :: new ( T :: ENCODING . to_string ( ) ) . unwrap ( ) ;
222
227
let size = mem:: size_of :: < T > ( ) ;
@@ -227,15 +232,18 @@ impl ClassDecl {
227
232
assert ! ( success != NO , "Failed to add ivar {}" , name) ;
228
233
}
229
234
230
- /// Adds a protocol to self. Panics if the protocol wasn't successfully
231
- /// added
235
+ /// Adds the given protocol to self.
236
+ ///
237
+ /// # Panics
238
+ ///
239
+ /// If the protocol wasn't successfully added.
232
240
pub fn add_protocol ( & mut self , proto : & Protocol ) {
233
241
let success = unsafe { runtime:: class_addProtocol ( self . cls , proto) } ;
234
242
assert ! ( success != NO , "Failed to add protocol {:?}" , proto) ;
235
243
}
236
244
237
- /// Registers self , consuming it and returning a reference to the
238
- /// newly registered `Class`.
245
+ /// Registers the [`ClassDecl`] , consuming it, and returns a reference to
246
+ /// the newly registered [ `Class`] .
239
247
pub fn register ( self ) -> & ' static Class {
240
248
unsafe {
241
249
let cls = self . cls ;
@@ -262,8 +270,9 @@ pub struct ProtocolDecl {
262
270
}
263
271
264
272
impl ProtocolDecl {
265
- /// Constructs a `ProtocolDecl` with the given name. Returns `None` if the
266
- /// protocol couldn't be allocated.
273
+ /// Constructs a [`ProtocolDecl`] with the given name.
274
+ ///
275
+ /// Returns [`None`] if the protocol couldn't be allocated.
267
276
pub fn new ( name : & str ) -> Option < ProtocolDecl > {
268
277
let c_name = CString :: new ( name) . unwrap ( ) ;
269
278
let proto = unsafe { runtime:: objc_allocateProtocol ( c_name. as_ptr ( ) ) } ;
@@ -303,7 +312,7 @@ impl ProtocolDecl {
303
312
}
304
313
}
305
314
306
- /// Adds an instance method declaration with a given description to self .
315
+ /// Adds an instance method declaration with a given description.
307
316
pub fn add_method_description < Args , Ret > ( & mut self , sel : Sel , is_required : bool )
308
317
where
309
318
Args : EncodeArguments ,
@@ -312,7 +321,7 @@ impl ProtocolDecl {
312
321
self . add_method_description_common :: < Args , Ret > ( sel, is_required, true )
313
322
}
314
323
315
- /// Adds a class method declaration with a given description to self .
324
+ /// Adds a class method declaration with a given description.
316
325
pub fn add_class_method_description < Args , Ret > ( & mut self , sel : Sel , is_required : bool )
317
326
where
318
327
Args : EncodeArguments ,
@@ -328,8 +337,8 @@ impl ProtocolDecl {
328
337
}
329
338
}
330
339
331
- /// Registers self , consuming it and returning a reference to the
332
- /// newly registered `Protocol`.
340
+ /// Registers the [`ProtocolDecl`] , consuming it and returning a reference
341
+ /// to the newly registered [ `Protocol`] .
333
342
pub fn register ( self ) -> & ' static Protocol {
334
343
unsafe {
335
344
runtime:: objc_registerProtocol ( self . proto ) ;
0 commit comments