@@ -76,19 +76,25 @@ pub struct Module {
7676impl From < ModuleBuilder < ' _ > > for Module {
7777 fn from ( builder : ModuleBuilder ) -> Self {
7878 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+
7990 Self {
8091 name : builder. name . into ( ) ,
8192 functions : functions
8293 . into_iter ( )
8394 . map ( Function :: from)
8495 . collect :: < StdVec < _ > > ( )
8596 . into ( ) ,
86- classes : builder
87- . classes
88- . into_iter ( )
89- . map ( |c| c ( ) . into ( ) )
90- . collect :: < StdVec < _ > > ( )
91- . into ( ) ,
97+ classes : classes. into ( ) ,
9298 constants : builder
9399 . constants
94100 . into_iter ( )
@@ -151,6 +157,8 @@ pub struct Parameter {
151157 pub ty : Option < DataType > ,
152158 /// Whether the parameter is nullable.
153159 pub nullable : bool ,
160+ /// Whether the parameter is variadic.
161+ pub variadic : bool ,
154162 /// Default value of the parameter.
155163 pub default : Option < RString > ,
156164}
@@ -175,6 +183,43 @@ pub struct Class {
175183 pub constants : Vec < Constant > ,
176184}
177185
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+
178223impl From < ClassBuilder > for Class {
179224 fn from ( val : ClassBuilder ) -> Self {
180225 Self {
@@ -426,6 +471,8 @@ impl From<(String, Box<dyn IntoConst + Send>, DocComments)> for Constant {
426471#[ cfg( test) ]
427472mod tests {
428473 #![ cfg_attr( windows, feature( abi_vectorcall) ) ]
474+ use cfg_if:: cfg_if;
475+
429476 use super :: * ;
430477
431478 use crate :: { args:: Arg , test:: test_function} ;
@@ -460,7 +507,13 @@ mod tests {
460507 let module: Module = builder. into ( ) ;
461508 assert_eq ! ( module. name, "test" . into( ) ) ;
462509 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+ }
464517 assert_eq ! ( module. constants. len( ) , 0 ) ;
465518 }
466519
@@ -479,6 +532,7 @@ mod tests {
479532 name: "foo" . into( ) ,
480533 ty: Option :: Some ( DataType :: Long ) ,
481534 nullable: false ,
535+ variadic: false ,
482536 default : Option :: None ,
483537 } ]
484538 . into( )
@@ -568,6 +622,7 @@ mod tests {
568622 name: "foo" . into( ) ,
569623 ty: Option :: Some ( DataType :: Long ) ,
570624 nullable: false ,
625+ variadic: false ,
571626 default : Option :: None ,
572627 } ]
573628 . into( )
0 commit comments