@@ -81,19 +81,25 @@ pub struct Module {
8181impl From < ModuleBuilder < ' _ > > for Module {
8282 fn from ( builder : ModuleBuilder ) -> Self {
8383 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+
8495 Self {
8596 name : builder. name . into ( ) ,
8697 functions : functions
8798 . into_iter ( )
8899 . map ( Function :: from)
89100 . collect :: < StdVec < _ > > ( )
90101 . into ( ) ,
91- classes : builder
92- . classes
93- . into_iter ( )
94- . map ( |c| c ( ) . into ( ) )
95- . collect :: < StdVec < _ > > ( )
96- . into ( ) ,
102+ classes : classes. into ( ) ,
97103 constants : builder
98104 . constants
99105 . into_iter ( )
@@ -163,6 +169,8 @@ pub struct Parameter {
163169 pub ty : Option < DataType > ,
164170 /// Whether the parameter is nullable.
165171 pub nullable : bool ,
172+ /// Whether the parameter is variadic.
173+ pub variadic : bool ,
166174 /// Default value of the parameter.
167175 pub default : Option < RString > ,
168176}
@@ -187,6 +195,43 @@ pub struct Class {
187195 pub constants : Vec < Constant > ,
188196}
189197
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+
190235impl From < ClassBuilder > for Class {
191236 fn from ( val : ClassBuilder ) -> Self {
192237 Self {
@@ -518,6 +563,8 @@ impl From<(String, Box<dyn IntoConst + Send>, DocComments)> for Constant {
518563#[ cfg( test) ]
519564mod tests {
520565 #![ cfg_attr( windows, feature( abi_vectorcall) ) ]
566+ use cfg_if:: cfg_if;
567+
521568 use super :: * ;
522569
523570 use crate :: { args:: Arg , test:: test_function} ;
@@ -554,7 +601,13 @@ mod tests {
554601 let module: Module = builder. into ( ) ;
555602 assert_eq ! ( module. name, "test" . into( ) ) ;
556603 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+ }
558611 assert_eq ! ( module. constants. len( ) , 0 ) ;
559612 }
560613
@@ -573,6 +626,7 @@ mod tests {
573626 name: "foo" . into( ) ,
574627 ty: Option :: Some ( DataType :: Long ) ,
575628 nullable: false ,
629+ variadic: false ,
576630 default : Option :: None ,
577631 } ]
578632 . into( )
@@ -662,6 +716,7 @@ mod tests {
662716 name: "foo" . into( ) ,
663717 ty: Option :: Some ( DataType :: Long ) ,
664718 nullable: false ,
719+ variadic: false ,
665720 default : Option :: None ,
666721 } ]
667722 . into( )
0 commit comments