@@ -77,11 +77,67 @@ macro_rules! __inner_declare_class {
77
77
@( $( $_kind: tt) * )
78
78
@( $( $args_start: tt) * )
79
79
@( $( $args_rest: tt) * )
80
+ } => {
81
+ $crate:: __fn_args! {
82
+ ( $crate:: __inner_declare_class)
83
+ ( $( $args_rest) * , )
84
+ ( )
85
+ ( )
86
+ @method_out_inner
87
+ @( $( #[ $( $m) * ] ) * )
88
+ @( $( $qualifiers) * )
89
+ @( $name)
90
+ @( $( $ret) ?)
91
+ @( $( $body) * )
92
+ @( $( $_kind) * )
93
+ @( $( $args_start) * )
94
+ // Will add @(args_converted)
95
+ // Will add @(body_prefix)
96
+ }
97
+ } ;
98
+
99
+ // No return type
100
+ {
101
+ @method_out_inner
102
+ @( $( #[ $( $m: tt) * ] ) * )
103
+ @( $( $qualifiers: tt) * )
104
+ @( $name: ident)
105
+ @( )
106
+ @( $( $body: tt) * )
107
+ @( $( $_kind: tt) * )
108
+ @( $( $args_start: tt) * )
109
+ @( $( $args_converted: tt) * )
110
+ @( $( $body_prefix: tt) * )
80
111
} => {
81
112
$crate:: __attribute_helper! {
82
113
@strip_sel
83
114
$( @[ $( $m) * ] ) *
84
- ( $( $qualifiers) * fn $name( $( $args_start) * $( $args_rest) * ) $( -> $ret) ? $( $body) * )
115
+ ( $( $qualifiers) * fn $name( $( $args_start) * $( $args_converted) * ) {
116
+ $( $body_prefix) *
117
+ $( $body) *
118
+ } )
119
+ }
120
+ } ;
121
+ // With return type
122
+ {
123
+ @method_out_inner
124
+ @( $( #[ $( $m: tt) * ] ) * )
125
+ @( $( $qualifiers: tt) * )
126
+ @( $name: ident)
127
+ @( $ret: ty)
128
+ @( $( $body: tt) * )
129
+ @( $( $_kind: tt) * )
130
+ @( $( $args_start: tt) * )
131
+ @( $( $args_converted: tt) * )
132
+ @( $( $body_prefix: tt) * )
133
+ } => {
134
+ $crate:: __attribute_helper! {
135
+ @strip_sel
136
+ $( @[ $( $m) * ] ) *
137
+ ( $( $qualifiers) * fn $name( $( $args_start) * $( $args_converted) * ) -> <$ret as $crate:: encode:: EncodeConvert >:: __Inner {
138
+ $( $body_prefix) *
139
+ <$ret as $crate:: encode:: EncodeConvert >:: __into_inner( $( $body) * )
140
+ } )
85
141
}
86
142
} ;
87
143
@@ -146,13 +202,86 @@ macro_rules! __inner_declare_class {
146
202
macro_rules! __fn_ptr {
147
203
(
148
204
@( $( $qualifiers: tt) * )
149
- $( $( $param: ident) ? $( _) ?: $param_ty: ty) ,* $( , ) ?
205
+ $( $( mut ) ? $ ( $param: ident) ? $( _) ?: $param_ty: ty) ,* $( , ) ?
150
206
) => {
151
207
$( $qualifiers) * fn ( $( $crate:: __fn_ptr!( @__to_anonymous $param_ty) ) ,* ) -> _
152
208
} ;
153
209
( @__to_anonymous $param_ty: ty) => { _ }
154
210
}
155
211
212
+ #[ doc( hidden) ]
213
+ #[ macro_export]
214
+ macro_rules! __fn_args {
215
+ // Ignore `_`
216
+ {
217
+ ( $out_macro: path)
218
+ ( _: $param_ty: ty, $( $rest: tt) * )
219
+ ( $( $args_converted: tt) * )
220
+ ( $( $body_prefix: tt) * )
221
+ $( $macro_args: tt) *
222
+ } => {
223
+ $crate:: __fn_args! {
224
+ ( $out_macro)
225
+ ( $( $rest) * )
226
+ ( $( $args_converted) * _: $param_ty, )
227
+ ( $( $body_prefix) * )
228
+ $( $macro_args) *
229
+ }
230
+ } ;
231
+ // Convert mut
232
+ {
233
+ ( $out_macro: path)
234
+ ( mut $param: ident: $param_ty: ty, $( $rest: tt) * )
235
+ ( $( $args_converted: tt) * )
236
+ ( $( $body_prefix: tt) * )
237
+ $( $macro_args: tt) *
238
+ } => {
239
+ $crate:: __fn_args! {
240
+ ( $out_macro)
241
+ ( $( $rest) * )
242
+ ( $( $args_converted) * $param: <$param_ty as $crate:: encode:: EncodeConvert >:: __Inner, )
243
+ (
244
+ $( $body_prefix) *
245
+ let mut $param = <$param_ty as $crate:: encode:: EncodeConvert >:: __from_inner( $param) ;
246
+ )
247
+ $( $macro_args) *
248
+ }
249
+ } ;
250
+ // Convert
251
+ {
252
+ ( $out_macro: path)
253
+ ( $param: ident: $param_ty: ty, $( $rest: tt) * )
254
+ ( $( $args_converted: tt) * )
255
+ ( $( $body_prefix: tt) * )
256
+ $( $macro_args: tt) *
257
+ } => {
258
+ $crate:: __fn_args! {
259
+ ( $out_macro)
260
+ ( $( $rest) * )
261
+ ( $( $args_converted) * $param: <$param_ty as $crate:: encode:: EncodeConvert >:: __Inner, )
262
+ (
263
+ $( $body_prefix) *
264
+ let $param = <$param_ty as $crate:: encode:: EncodeConvert >:: __from_inner( $param) ;
265
+ )
266
+ $( $macro_args) *
267
+ }
268
+ } ;
269
+ // Output result
270
+ {
271
+ ( $out_macro: path)
272
+ ( $( , ) * )
273
+ ( $( $args_converted: tt) * )
274
+ ( $( $body_prefix: tt) * )
275
+ $( $macro_args: tt) *
276
+ } => {
277
+ $out_macro! {
278
+ $( $macro_args) *
279
+ @( $( $args_converted) * )
280
+ @( $( $body_prefix) * )
281
+ }
282
+ } ;
283
+ }
284
+
156
285
/// Declare a new Objective-C class.
157
286
///
158
287
/// This is mostly just a convenience macro on top of [`extern_class!`] and
@@ -205,10 +334,15 @@ macro_rules! __fn_ptr {
205
334
/// can't mark them as `pub` for the same reason). Instead, use the
206
335
/// [`extern_methods!`] macro to create a Rust interface to the methods.
207
336
///
337
+ /// If the argument or return type is [`bool`], a conversion is performed to
338
+ /// make it behave similarly to the Objective-C `BOOL`. Use [`runtime::Bool`]
339
+ /// if you want to control this manually.
340
+ ///
208
341
/// ["associated functions"]: https://doc.rust-lang.org/reference/items/associated-items.html#methods
209
342
/// ["methods"]: https://doc.rust-lang.org/reference/items/associated-items.html#methods
210
343
/// [`extern_methods!`]: crate::extern_methods
211
344
/// [`msg_send!`]: crate::msg_send
345
+ /// [`runtime::Bool`]: crate::runtime::Bool
212
346
///
213
347
///
214
348
/// ## Protocol definitions
@@ -252,7 +386,6 @@ macro_rules! __fn_ptr {
252
386
/// use std::os::raw::c_int;
253
387
/// use objc2::rc::{Id, Owned};
254
388
/// use objc2::foundation::{NSCopying, NSObject, NSZone};
255
- /// use objc2::runtime::Bool;
256
389
/// use objc2::{declare_class, msg_send, msg_send_id, ClassType};
257
390
/// #
258
391
/// # #[cfg(feature = "gnustep-1-7")]
@@ -290,8 +423,8 @@ macro_rules! __fn_ptr {
290
423
/// }
291
424
///
292
425
/// #[sel(myClassMethod)]
293
- /// fn __my_class_method() -> Bool {
294
- /// Bool::YES
426
+ /// fn __my_class_method() -> bool {
427
+ /// true
295
428
/// }
296
429
/// }
297
430
///
0 commit comments