@@ -76,19 +76,25 @@ pub struct Module {
76
76
impl From < ModuleBuilder < ' _ > > for Module {
77
77
fn from ( builder : ModuleBuilder ) -> Self {
78
78
let functions = builder. functions ;
79
+
80
+ #[ allow( unused_mut) ]
81
+ let mut classes = builder
82
+ . classes
83
+ . into_iter ( )
84
+ . map ( |c| c ( ) . into ( ) )
85
+ . collect :: < StdVec < _ > > ( ) ;
86
+
87
+ #[ cfg( feature = "closure" ) ]
88
+ classes. push ( Class :: closure ( ) ) ;
89
+
79
90
Self {
80
91
name : builder. name . into ( ) ,
81
92
functions : functions
82
93
. into_iter ( )
83
94
. map ( Function :: from)
84
95
. collect :: < StdVec < _ > > ( )
85
96
. into ( ) ,
86
- classes : builder
87
- . classes
88
- . into_iter ( )
89
- . map ( |c| c ( ) . into ( ) )
90
- . collect :: < StdVec < _ > > ( )
91
- . into ( ) ,
97
+ classes : classes. into ( ) ,
92
98
constants : builder
93
99
. constants
94
100
. into_iter ( )
@@ -151,6 +157,8 @@ pub struct Parameter {
151
157
pub ty : Option < DataType > ,
152
158
/// Whether the parameter is nullable.
153
159
pub nullable : bool ,
160
+ /// Whether the parameter is variadic.
161
+ pub variadic : bool ,
154
162
/// Default value of the parameter.
155
163
pub default : Option < RString > ,
156
164
}
@@ -175,6 +183,43 @@ pub struct Class {
175
183
pub constants : Vec < Constant > ,
176
184
}
177
185
186
+ #[ cfg( feature = "closure" ) ]
187
+ impl Class {
188
+ /// Creates a new class representing a Rust closure used for generating
189
+ /// the stubs if the `closure` feature is enabled.
190
+ #[ must_use]
191
+ pub fn closure ( ) -> Self {
192
+ Self {
193
+ name : "RustClosure" . into ( ) ,
194
+ docs : DocBlock ( StdVec :: new ( ) . into ( ) ) ,
195
+ extends : Option :: None ,
196
+ implements : StdVec :: new ( ) . into ( ) ,
197
+ properties : StdVec :: new ( ) . into ( ) ,
198
+ methods : vec ! [ Method {
199
+ name: "__invoke" . into( ) ,
200
+ docs: DocBlock ( StdVec :: new( ) . into( ) ) ,
201
+ ty: MethodType :: Member ,
202
+ params: vec![ Parameter {
203
+ name: "args" . into( ) ,
204
+ ty: Option :: Some ( DataType :: Mixed ) ,
205
+ nullable: false ,
206
+ variadic: true ,
207
+ default : Option :: None ,
208
+ } ]
209
+ . into( ) ,
210
+ retval: Option :: Some ( Retval {
211
+ ty: DataType :: Mixed ,
212
+ nullable: false ,
213
+ } ) ,
214
+ r#static: false ,
215
+ visibility: Visibility :: Public ,
216
+ } ]
217
+ . into ( ) ,
218
+ constants : StdVec :: new ( ) . into ( ) ,
219
+ }
220
+ }
221
+ }
222
+
178
223
impl From < ClassBuilder > for Class {
179
224
fn from ( val : ClassBuilder ) -> Self {
180
225
Self {
@@ -426,6 +471,8 @@ impl From<(String, Box<dyn IntoConst + Send>, DocComments)> for Constant {
426
471
#[ cfg( test) ]
427
472
mod tests {
428
473
#![ cfg_attr( windows, feature( abi_vectorcall) ) ]
474
+ use cfg_if:: cfg_if;
475
+
429
476
use super :: * ;
430
477
431
478
use crate :: { args:: Arg , test:: test_function} ;
@@ -460,7 +507,13 @@ mod tests {
460
507
let module: Module = builder. into ( ) ;
461
508
assert_eq ! ( module. name, "test" . into( ) ) ;
462
509
assert_eq ! ( module. functions. len( ) , 1 ) ;
463
- assert_eq ! ( module. classes. len( ) , 0 ) ;
510
+ cfg_if ! {
511
+ if #[ cfg( feature = "closure" ) ] {
512
+ assert_eq!( module. classes. len( ) , 1 ) ;
513
+ } else {
514
+ assert_eq!( module. classes. len( ) , 0 ) ;
515
+ }
516
+ }
464
517
assert_eq ! ( module. constants. len( ) , 0 ) ;
465
518
}
466
519
@@ -479,6 +532,7 @@ mod tests {
479
532
name: "foo" . into( ) ,
480
533
ty: Option :: Some ( DataType :: Long ) ,
481
534
nullable: false ,
535
+ variadic: false ,
482
536
default : Option :: None ,
483
537
} ]
484
538
. into( )
@@ -568,6 +622,7 @@ mod tests {
568
622
name: "foo" . into( ) ,
569
623
ty: Option :: Some ( DataType :: Long ) ,
570
624
nullable: false ,
625
+ variadic: false ,
571
626
default : Option :: None ,
572
627
} ]
573
628
. into( )
0 commit comments