@@ -14,8 +14,8 @@ use crate::type_id::Crate;
14
14
use crate :: { derive, generics} ;
15
15
use proc_macro2:: { Ident , Span , TokenStream } ;
16
16
use quote:: { format_ident, quote, quote_spanned, ToTokens } ;
17
- use syn:: spanned:: Spanned ;
18
17
use std:: mem;
18
+ use syn:: spanned:: Spanned ;
19
19
use syn:: { parse_quote, punctuated, Generics , Lifetime , Result , Token } ;
20
20
21
21
pub ( crate ) fn bridge ( mut ffi : Module ) -> Result < TokenStream > {
@@ -87,7 +87,12 @@ fn expand(ffi: Module, doc: Doc, attrs: OtherAttrs, apis: &[Api], types: &Types)
87
87
expanded. extend ( expand_rust_type_impl ( ety) ) ;
88
88
hidden. extend ( expand_rust_type_layout ( ety, types) ) ;
89
89
}
90
- Api :: RustFunction ( efn) => hidden. extend ( expand_rust_function_shim ( efn, types) ) ,
90
+ Api :: RustFunction ( efn) => {
91
+ if efn. asyncness . is_some ( ) {
92
+ // todo!("expand_rust_function_shim\n{}", expand_rust_function_shim(efn, types).to_string());
93
+ }
94
+ hidden. extend ( expand_rust_function_shim ( efn, types) ) ;
95
+ }
91
96
Api :: TypeAlias ( alias) => {
92
97
expanded. extend ( expand_type_alias ( alias) ) ;
93
98
hidden. extend ( expand_type_alias_verify ( alias, types) ) ;
@@ -723,10 +728,10 @@ fn expand_cxx_function_shim(efn: &ExternFn, types: &Types) -> TokenStream {
723
728
false => quote_spanned ! ( span=> #call. as_slice:: <#inner>( ) ) ,
724
729
true => quote_spanned ! ( span=> #call. as_mut_slice:: <#inner>( ) ) ,
725
730
}
726
- } ,
731
+ }
727
732
Type :: Future ( _) => {
728
733
quote_spanned ! ( span=> :: kj_rs:: new_callbacks_promise_future( #call) )
729
- } ,
734
+ }
730
735
_ => call,
731
736
} ,
732
737
} ;
@@ -1115,6 +1120,11 @@ fn expand_rust_function_shim_impl(
1115
1120
false => Some ( quote_spanned ! ( span=> :: cxx:: private:: RustSlice :: from_ref) ) ,
1116
1121
true => Some ( quote_spanned ! ( span=> :: cxx:: private:: RustSlice :: from_mut) ) ,
1117
1122
} ,
1123
+ Type :: Future ( fut) => if fut. throws_tokens . is_some ( ) {
1124
+ Some ( quote_spanned ! ( span=> :: kj_rs:: repr:: future) )
1125
+ } else {
1126
+ Some ( quote_spanned ! ( span=> :: kj_rs:: repr:: infallible_future) )
1127
+ } ,
1118
1128
_ => None ,
1119
1129
} ) ;
1120
1130
@@ -1132,7 +1142,17 @@ fn expand_rust_function_shim_impl(
1132
1142
1133
1143
// always indirect return when there's a return value
1134
1144
let out_param = sig. ret . as_ref ( ) . map ( |ret| {
1135
- let ret = expand_extern_type ( ret, types, false ) ;
1145
+ let ret = if let Type :: Future ( fut) = ret {
1146
+ let span = sig. ret . span ( ) ;
1147
+ let output = & fut. output ;
1148
+ if fut. throws_tokens . is_some ( ) {
1149
+ quote_spanned ! ( span=>:: kj_rs:: repr:: RustFuture :: <#output>)
1150
+ } else {
1151
+ quote_spanned ! ( span=>:: kj_rs:: repr:: RustInfallibleFuture :: <#output>)
1152
+ }
1153
+ } else {
1154
+ expand_extern_type ( ret, types, false )
1155
+ } ;
1136
1156
quote_spanned ! ( span=> __return: * mut #ret, )
1137
1157
} ) ;
1138
1158
let out = match sig. ret {
@@ -1223,6 +1243,13 @@ fn expand_rust_function_shim_super(
1223
1243
quote_spanned ! ( rangle. span=> :: cxx:: core:: fmt:: Display >)
1224
1244
} ;
1225
1245
quote ! ( -> #result_begin #result_end)
1246
+ } else if let Some ( Type :: Future ( fut) ) = & sig. ret {
1247
+ let output = & fut. output ;
1248
+ if fut. throws_tokens . is_some ( ) {
1249
+ quote ! ( -> std:: pin:: Pin <Box <dyn :: std:: future:: Future <Output = :: std:: result:: Result <#output, String >> + Send >>)
1250
+ } else {
1251
+ quote ! ( -> std:: pin:: Pin <Box <dyn :: std:: future:: Future <Output = #output> + Send >>)
1252
+ }
1226
1253
} else {
1227
1254
expand_return_type ( & sig. ret )
1228
1255
} ;
@@ -1239,7 +1266,15 @@ fn expand_rust_function_shim_super(
1239
1266
}
1240
1267
} ;
1241
1268
1242
- let mut body = quote_spanned ! ( span=> #call( #( #vars, ) * ) ) ;
1269
+ let mut body = if let Some ( Type :: Future ( fut) ) = & sig. ret {
1270
+ if fut. throws_tokens . is_some ( ) {
1271
+ quote_spanned ! ( span=> Box :: pin( async move { #call( #( #vars, ) * ) . await . map_err( |e| e. to_string( ) ) } ) )
1272
+ } else {
1273
+ quote_spanned ! ( span=> Box :: pin( #call( #( #vars, ) * ) ) )
1274
+ }
1275
+ } else {
1276
+ quote_spanned ! ( span=> #call( #( #vars, ) * ) )
1277
+ } ;
1243
1278
let mut allow_unused_unsafe = None ;
1244
1279
if unsafety. is_some ( ) {
1245
1280
body = quote_spanned ! ( span=> unsafe { #body } ) ;
@@ -1939,8 +1974,8 @@ fn expand_extern_type(ty: &Type, types: &Types, proper: bool) -> TokenStream {
1939
1974
let rust_slice = Ident :: new ( "RustSlice" , ty. bracket . span . join ( ) ) ;
1940
1975
quote_spanned ! ( span=> :: cxx:: private:: #rust_slice)
1941
1976
}
1942
- Type :: Future ( ty ) => {
1943
- let span = ty . span ( ) ;
1977
+ Type :: Future ( output ) => {
1978
+ let span = output . span ( ) ;
1944
1979
quote_spanned ! ( span=> :: kj_rs:: KjPromiseNodeImpl )
1945
1980
}
1946
1981
_ => quote ! ( #ty) ,
0 commit comments