1
1
//! Functionality for declaring Objective-C classes.
2
2
//!
3
- //! Classes can be declared using the [`ClassDecl `] struct. Instance variables
3
+ //! Classes can be declared using the [`ClassBuilder `] struct. Instance variables
4
4
//! and methods can then be added before the class is ultimately registered.
5
5
//!
6
6
//! # Example
11
11
//!
12
12
//! ```no_run
13
13
//! use objc2::{class, sel};
14
- //! use objc2::declare::ClassDecl ;
14
+ //! use objc2::declare::ClassBuilder ;
15
15
//! use objc2::runtime::{Class, Object, Sel};
16
16
//!
17
17
//! let superclass = class!(NSObject);
18
- //! let mut decl = ClassDecl ::new("MyNumber", superclass).unwrap();
18
+ //! let mut decl = ClassBuilder ::new("MyNumber", superclass).unwrap();
19
19
//!
20
20
//! // Add an instance variable
21
21
//! decl.add_ivar::<u32>("_number");
@@ -78,6 +78,10 @@ macro_rules! method_decl_impl {
78
78
( $( $t: ident) ,* ) => (
79
79
method_decl_impl!( -T , R , extern "C" fn ( & T , Sel $( , $t) * ) -> R , $( $t) ,* ) ;
80
80
method_decl_impl!( -T , R , extern "C" fn ( & mut T , Sel $( , $t) * ) -> R , $( $t) ,* ) ;
81
+ method_decl_impl!( -T , R , unsafe extern "C" fn ( * const T , Sel $( , $t) * ) -> R , $( $t) ,* ) ;
82
+ method_decl_impl!( -T , R , unsafe extern "C" fn ( * mut T , Sel $( , $t) * ) -> R , $( $t) ,* ) ;
83
+ method_decl_impl!( -T , R , unsafe extern "C" fn ( & T , Sel $( , $t) * ) -> R , $( $t) ,* ) ;
84
+ method_decl_impl!( -T , R , unsafe extern "C" fn ( & mut T , Sel $( , $t) * ) -> R , $( $t) ,* ) ;
81
85
) ;
82
86
}
83
87
@@ -120,10 +124,14 @@ fn log2_align_of<T>() -> u8 {
120
124
/// A type for declaring a new class and adding new methods and ivars to it
121
125
/// before registering it.
122
126
#[ derive( Debug ) ]
123
- pub struct ClassDecl {
127
+ pub struct ClassBuilder {
124
128
cls : NonNull < Class > ,
125
129
}
126
130
131
+ #[ doc( hidden) ]
132
+ #[ deprecated = "Use `ClassBuilder` instead." ]
133
+ pub type ClassDecl = ClassBuilder ;
134
+
127
135
// SAFETY: The stuff that touch global state does so using locks internally.
128
136
//
129
137
// Modifying the class itself can only be done through `&mut`, so Sync is
@@ -134,11 +142,11 @@ pub struct ClassDecl {
134
142
// when doing so...).
135
143
//
136
144
// Finally, there are no requirements that the class must be registered on the
137
- // same thread that allocated it.
138
- unsafe impl Send for ClassDecl { }
139
- unsafe impl Sync for ClassDecl { }
145
+ // same thread that allocated it (so Send is safe) .
146
+ unsafe impl Send for ClassBuilder { }
147
+ unsafe impl Sync for ClassBuilder { }
140
148
141
- impl ClassDecl {
149
+ impl ClassBuilder {
142
150
fn as_ptr ( & self ) -> * mut ffi:: objc_class {
143
151
self . cls . as_ptr ( ) . cast ( )
144
152
}
@@ -150,16 +158,16 @@ impl ClassDecl {
150
158
NonNull :: new ( cls. cast ( ) ) . map ( |cls| Self { cls } )
151
159
}
152
160
153
- /// Constructs a [`ClassDecl `] with the given name and superclass.
161
+ /// Constructs a [`ClassBuilder `] with the given name and superclass.
154
162
///
155
163
/// Returns [`None`] if the class couldn't be allocated, or a class with
156
164
/// that name already exist.
157
165
pub fn new ( name : & str , superclass : & Class ) -> Option < Self > {
158
166
Self :: with_superclass ( name, Some ( superclass) )
159
167
}
160
168
161
- /// Constructs a [`ClassDecl `] declaring a new root class with the given
162
- /// name.
169
+ /// Constructs a [`ClassBuilder `] declaring a new root class with the
170
+ /// given name.
163
171
///
164
172
/// Returns [`None`] if the class couldn't be allocated.
165
173
///
@@ -173,11 +181,10 @@ impl ClassDecl {
173
181
/// Functionality it expects, like implementations of `-retain` and
174
182
/// `-release` used by ARC, will not be present otherwise.
175
183
pub fn root ( name : & str , intitialize_fn : extern "C" fn ( & Class , Sel ) ) -> Option < Self > {
176
- let mut decl = Self :: with_superclass ( name, None ) ;
177
- if let Some ( ref mut decl) = decl {
178
- unsafe { decl. add_class_method ( sel ! ( initialize) , intitialize_fn) } ;
179
- }
180
- decl
184
+ Self :: with_superclass ( name, None ) . map ( |mut this| {
185
+ unsafe { this. add_class_method ( sel ! ( initialize) , intitialize_fn) } ;
186
+ this
187
+ } )
181
188
}
182
189
183
190
/// Adds a method with the given name and implementation.
@@ -191,9 +198,10 @@ impl ClassDecl {
191
198
///
192
199
/// The caller must ensure that the types match those that are expected
193
200
/// when the method is invoked from Objective-C.
194
- pub unsafe fn add_method < F > ( & mut self , sel : Sel , func : F )
201
+ pub unsafe fn add_method < T , F > ( & mut self , sel : Sel , func : F )
195
202
where
196
- F : MethodImplementation < Callee = Object > ,
203
+ T : Message + ?Sized , // TODO: Disallow `Class`
204
+ F : MethodImplementation < Callee = T > ,
197
205
{
198
206
let encs = F :: Args :: ENCODINGS ;
199
207
let sel_args = count_args ( sel) ;
@@ -290,8 +298,8 @@ impl ClassDecl {
290
298
291
299
// fn add_property(&self, name: &str, attributes: &[ffi::objc_property_attribute_t]);
292
300
293
- /// Registers the [`ClassDecl `], consuming it, and returns a reference to
294
- /// the newly registered [`Class`].
301
+ /// Registers the [`ClassBuilder `], consuming it, and returns a reference
302
+ /// to the newly registered [`Class`].
295
303
pub fn register ( self ) -> & ' static Class {
296
304
// Forget self, otherwise the class will be disposed in drop
297
305
let cls = ManuallyDrop :: new ( self ) . cls ;
@@ -300,7 +308,7 @@ impl ClassDecl {
300
308
}
301
309
}
302
310
303
- impl Drop for ClassDecl {
311
+ impl Drop for ClassBuilder {
304
312
fn drop ( & mut self ) {
305
313
unsafe { ffi:: objc_disposeClassPair ( self . as_ptr ( ) ) }
306
314
}
@@ -309,20 +317,24 @@ impl Drop for ClassDecl {
309
317
/// A type for declaring a new protocol and adding new methods to it
310
318
/// before registering it.
311
319
#[ derive( Debug ) ]
312
- pub struct ProtocolDecl {
320
+ pub struct ProtocolBuilder {
313
321
proto : NonNull < Protocol > ,
314
322
}
315
323
316
- // SAFETY: Similar to ClassDecl
317
- unsafe impl Send for ProtocolDecl { }
318
- unsafe impl Sync for ProtocolDecl { }
324
+ #[ doc( hidden) ]
325
+ #[ deprecated = "Use `ProtocolBuilder` instead." ]
326
+ pub type ProtocolDecl = ProtocolBuilder ;
327
+
328
+ // SAFETY: Similar to ClassBuilder
329
+ unsafe impl Send for ProtocolBuilder { }
330
+ unsafe impl Sync for ProtocolBuilder { }
319
331
320
- impl ProtocolDecl {
332
+ impl ProtocolBuilder {
321
333
fn as_ptr ( & self ) -> * mut ffi:: objc_protocol {
322
334
self . proto . as_ptr ( ) . cast ( )
323
335
}
324
336
325
- /// Constructs a [`ProtocolDecl `] with the given name.
337
+ /// Constructs a [`ProtocolBuilder `] with the given name.
326
338
///
327
339
/// Returns [`None`] if the protocol couldn't be allocated.
328
340
pub fn new ( name : & str ) -> Option < Self > {
@@ -386,7 +398,7 @@ impl ProtocolDecl {
386
398
}
387
399
}
388
400
389
- /// Registers the [`ProtocolDecl `], consuming it and returning a reference
401
+ /// Registers the [`ProtocolBuilder `], consuming it and returning a reference
390
402
/// to the newly registered [`Protocol`].
391
403
pub fn register ( self ) -> & ' static Protocol {
392
404
unsafe {
0 commit comments