@@ -81,19 +81,25 @@ pub struct Module {
81
81
impl From < ModuleBuilder < ' _ > > for Module {
82
82
fn from ( builder : ModuleBuilder ) -> Self {
83
83
let functions = builder. functions ;
84
+
85
+ #[ allow( unused_mut) ]
86
+ let mut classes = builder
87
+ . classes
88
+ . into_iter ( )
89
+ . map ( |c| c ( ) . into ( ) )
90
+ . collect :: < StdVec < _ > > ( ) ;
91
+
92
+ #[ cfg( feature = "closure" ) ]
93
+ classes. push ( Class :: closure ( ) ) ;
94
+
84
95
Self {
85
96
name : builder. name . into ( ) ,
86
97
functions : functions
87
98
. into_iter ( )
88
99
. map ( Function :: from)
89
100
. collect :: < StdVec < _ > > ( )
90
101
. into ( ) ,
91
- classes : builder
92
- . classes
93
- . into_iter ( )
94
- . map ( |c| c ( ) . into ( ) )
95
- . collect :: < StdVec < _ > > ( )
96
- . into ( ) ,
102
+ classes : classes. into ( ) ,
97
103
constants : builder
98
104
. constants
99
105
. into_iter ( )
@@ -163,6 +169,8 @@ pub struct Parameter {
163
169
pub ty : Option < DataType > ,
164
170
/// Whether the parameter is nullable.
165
171
pub nullable : bool ,
172
+ /// Whether the parameter is variadic.
173
+ pub variadic : bool ,
166
174
/// Default value of the parameter.
167
175
pub default : Option < RString > ,
168
176
}
@@ -187,6 +195,43 @@ pub struct Class {
187
195
pub constants : Vec < Constant > ,
188
196
}
189
197
198
+ #[ cfg( feature = "closure" ) ]
199
+ impl Class {
200
+ /// Creates a new class representing a Rust closure used for generating
201
+ /// the stubs if the `closure` feature is enabled.
202
+ #[ must_use]
203
+ pub fn closure ( ) -> Self {
204
+ Self {
205
+ name : "RustClosure" . into ( ) ,
206
+ docs : DocBlock ( StdVec :: new ( ) . into ( ) ) ,
207
+ extends : Option :: None ,
208
+ implements : StdVec :: new ( ) . into ( ) ,
209
+ properties : StdVec :: new ( ) . into ( ) ,
210
+ methods : vec ! [ Method {
211
+ name: "__invoke" . into( ) ,
212
+ docs: DocBlock ( StdVec :: new( ) . into( ) ) ,
213
+ ty: MethodType :: Member ,
214
+ params: vec![ Parameter {
215
+ name: "args" . into( ) ,
216
+ ty: Option :: Some ( DataType :: Mixed ) ,
217
+ nullable: false ,
218
+ variadic: true ,
219
+ default : Option :: None ,
220
+ } ]
221
+ . into( ) ,
222
+ retval: Option :: Some ( Retval {
223
+ ty: DataType :: Mixed ,
224
+ nullable: false ,
225
+ } ) ,
226
+ r#static: false ,
227
+ visibility: Visibility :: Public ,
228
+ } ]
229
+ . into ( ) ,
230
+ constants : StdVec :: new ( ) . into ( ) ,
231
+ }
232
+ }
233
+ }
234
+
190
235
impl From < ClassBuilder > for Class {
191
236
fn from ( val : ClassBuilder ) -> Self {
192
237
Self {
@@ -518,6 +563,8 @@ impl From<(String, Box<dyn IntoConst + Send>, DocComments)> for Constant {
518
563
#[ cfg( test) ]
519
564
mod tests {
520
565
#![ cfg_attr( windows, feature( abi_vectorcall) ) ]
566
+ use cfg_if:: cfg_if;
567
+
521
568
use super :: * ;
522
569
523
570
use crate :: { args:: Arg , test:: test_function} ;
@@ -554,7 +601,13 @@ mod tests {
554
601
let module: Module = builder. into ( ) ;
555
602
assert_eq ! ( module. name, "test" . into( ) ) ;
556
603
assert_eq ! ( module. functions. len( ) , 1 ) ;
557
- assert_eq ! ( module. classes. len( ) , 0 ) ;
604
+ cfg_if ! {
605
+ if #[ cfg( feature = "closure" ) ] {
606
+ assert_eq!( module. classes. len( ) , 1 ) ;
607
+ } else {
608
+ assert_eq!( module. classes. len( ) , 0 ) ;
609
+ }
610
+ }
558
611
assert_eq ! ( module. constants. len( ) , 0 ) ;
559
612
}
560
613
@@ -573,6 +626,7 @@ mod tests {
573
626
name: "foo" . into( ) ,
574
627
ty: Option :: Some ( DataType :: Long ) ,
575
628
nullable: false ,
629
+ variadic: false ,
576
630
default : Option :: None ,
577
631
} ]
578
632
. into( )
@@ -662,6 +716,7 @@ mod tests {
662
716
name: "foo" . into( ) ,
663
717
ty: Option :: Some ( DataType :: Long ) ,
664
718
nullable: false ,
719
+ variadic: false ,
665
720
default : Option :: None ,
666
721
} ]
667
722
. into( )
0 commit comments