@@ -152,7 +152,7 @@ fn proxy_wrap_function_def(
152152 } ;
153153
154154 let func_name = & f. sig . ident ;
155- let ( mut original_arg_idents, _ ) = f
155+ let ( mut original_arg_idents, mut original_arg_types ) = f
156156 . sig
157157 . inputs
158158 . iter ( )
@@ -190,6 +190,7 @@ fn proxy_wrap_function_def(
190190
191191 let args_var_to_use = if get_world_callback_access_fn. is_some ( ) {
192192 original_arg_idents. remove ( 0 ) ;
193+ original_arg_types. remove ( 0 ) ;
193194 args_tail_ident
194195 } else {
195196 args_ident
@@ -203,7 +204,7 @@ fn proxy_wrap_function_def(
203204 } ;
204205
205206 // wrap function body in our unwrapping and wrapping logic, ignore pre-existing body
206- let mut fn_call = std:: panic:: catch_unwind ( || {
207+ let mut fn_call = // std::panic::catch_unwind(|| {
207208 match (
208209 & f. default ,
209210 & attrs. as_trait ,
@@ -212,9 +213,19 @@ fn proxy_wrap_function_def(
212213 ( _, _, true ) => quote_spanned ! ( span=>
213214 world. #func_name( #( #original_arg_idents) , * )
214215 ) ,
215- ( Some ( body) , _, _) => quote_spanned ! ( span=>
216- ( ||{ #body } ) ( )
217- ) ,
216+ ( Some ( body) , _, _) => {
217+ if attrs. no_proxy . is_present ( ) {
218+ // in this case the entire thing is just this
219+ quote_spanned ! ( span=>
220+ { ( |#ctxt_arg_ident, ( #( #original_arg_idents) , * ) : ( #( #original_arg_types) , * ) |{ #body } ) ( #ctxt_alias, #args_var_to_use) }
221+ )
222+ } else {
223+ // in this case we are just using this to contain any ? calls and early returns
224+ quote_spanned ! ( span=>
225+ ( || { #body } ) ( )
226+ )
227+ }
228+ } ,
218229 ( _, None , _) => quote_spanned ! ( span=>
219230 #target_type:: #func_name( #( #original_arg_idents) , * )
220231 ) ,
@@ -224,9 +235,9 @@ fn proxy_wrap_function_def(
224235 <#target_type as #trait_path>:: #func_name( #( #original_arg_idents) , * )
225236 )
226237 }
227- }
228- } )
229- . unwrap ( ) ; // todo: handle the error nicer
238+ } ;
239+ // })
240+ // .unwrap(); // todo: handle the error nicer
230241
231242 if f. sig . unsafety . is_some ( ) {
232243 fn_call = quote_spanned ! ( span=>
@@ -236,9 +247,9 @@ fn proxy_wrap_function_def(
236247
237248 if attrs. no_proxy . is_present ( ) {
238249 f. default = Some ( parse_quote_spanned ! { span=>
239- {
240- #fn_call
241- }
250+
251+ #fn_call
252+
242253 } ) ;
243254 } else {
244255 let world = if let Some ( world_getter_fn_path) = get_world_callback_access_fn {
@@ -254,11 +265,17 @@ fn proxy_wrap_function_def(
254265 )
255266 } ;
256267
268+ let proxy_call_name: Ident = if attrs. fallible . is_present ( ) {
269+ format_ident ! ( "try_proxy_call" , span = span)
270+ } else {
271+ format_ident ! ( "proxy_call" , span = span)
272+ } ;
273+
257274 f. default = Some ( parse_quote_spanned ! { span=>
258275 {
259276 #args_split
260277 #world
261- let out: #out_type = world. proxy_call ( #args_var_to_use, |( #( #original_arg_idents) , * ) | {
278+ let out: #out_type = world. #proxy_call_name ( #args_var_to_use, |( #( #original_arg_idents) , * ) | {
262279 #fn_call
263280 } ) . map_err( |e| #mlua:: Error :: external( e) ) ?;
264281 Ok ( out)
@@ -315,6 +332,9 @@ struct FunctionAttrs {
315332 /// i.e. will remove that argument from the function signature and use it's name as the context alias
316333 pub with_context : Flag ,
317334
335+ /// if true will treat output as a fallible result
336+ pub fallible : Flag ,
337+
318338 /// Skips the unproxying & proxying call, useful for functions that don't need to access the world
319339 pub no_proxy : Flag ,
320340}
@@ -377,7 +397,7 @@ pub fn impl_lua_proxy(input: TokenStream) -> TokenStream {
377397 // extract composites first
378398 let mut composites: HashMap < String , Vec < ( TraitItemFn , FunctionAttrs ) > > = HashMap :: new ( ) ;
379399 meta. functions . 0 . retain ( |f| {
380- let attrs = FunctionAttrs :: from_attributes ( & f. attrs ) . unwrap ( ) ;
400+ let attrs = FunctionAttrs :: from_attributes ( & f. attrs ) . expect ( "Function attributes must be valid" ) ;
381401 if let Some ( composite_id) = & attrs. composite {
382402 composites
383403 . entry ( composite_id. to_owned ( ) )
@@ -423,7 +443,7 @@ pub fn impl_lua_proxy(input: TokenStream) -> TokenStream {
423443 . collect :: < Vec < _ > > ( ) ;
424444
425445 let closure_args_types = closures. iter ( ) . map ( |closure| {
426- let last = closure. inputs . last ( ) . unwrap ( ) ;
446+ let last = closure. inputs . last ( ) . expect ( "Closure must have at least one argument" ) ;
427447 if let syn:: Pat :: Type ( pat_type) = last {
428448 & pat_type. ty
429449 } else {
@@ -448,7 +468,7 @@ pub fn impl_lua_proxy(input: TokenStream) -> TokenStream {
448468 } ) ;
449469
450470 let add_function_stmts = meta. functions . 0 . into_iter ( ) . filter_map ( |mut f| {
451- let attrs = FunctionAttrs :: from_attributes ( & f. attrs ) . unwrap ( ) ;
471+ let attrs = FunctionAttrs :: from_attributes ( & f. attrs ) . expect ( "Function attributes must be valid" ) ;
452472
453473 if attrs. skip . is_present ( ) {
454474 return None ;
0 commit comments