8181
8282use std:: cell:: RefCell ;
8383use std:: os:: raw:: { c_int, c_void} ;
84+ use std:: result:: Result as StdResult ;
8485use std:: { mem, ptr, slice} ;
8586
86- use crate :: error:: { Error , Result } ;
87+ use crate :: error:: { Error , ExternalError , ExternalResult , Result } ;
8788use crate :: state:: Lua ;
8889use crate :: table:: Table ;
8990use crate :: traits:: { FromLuaMulti , IntoLua , IntoLuaMulti } ;
@@ -635,30 +636,32 @@ impl Function {
635636 /// Wraps a Rust function or closure, returning an opaque type that implements [`IntoLua`]
636637 /// trait.
637638 #[ inline]
638- pub fn wrap < F , A , R > ( func : F ) -> impl IntoLua
639+ pub fn wrap < F , A , R , E > ( func : F ) -> impl IntoLua
639640 where
640- F : LuaNativeFn < A , Output = Result < R > > + MaybeSend + ' static ,
641+ F : LuaNativeFn < A , Output = StdResult < R , E > > + MaybeSend + ' static ,
641642 A : FromLuaMulti ,
642643 R : IntoLuaMulti ,
644+ E : ExternalError ,
643645 {
644646 WrappedFunction ( Box :: new ( move |lua, nargs| unsafe {
645647 let args = A :: from_stack_args ( nargs, 1 , None , lua) ?;
646- func. call ( args) ?. push_into_stack_multi ( lua)
648+ func. call ( args) . into_lua_err ( ) ?. push_into_stack_multi ( lua)
647649 } ) )
648650 }
649651
650652 /// Wraps a Rust mutable closure, returning an opaque type that implements [`IntoLua`] trait.
651- pub fn wrap_mut < F , A , R > ( func : F ) -> impl IntoLua
653+ pub fn wrap_mut < F , A , R , E > ( func : F ) -> impl IntoLua
652654 where
653- F : LuaNativeFnMut < A , Output = Result < R > > + MaybeSend + ' static ,
655+ F : LuaNativeFnMut < A , Output = StdResult < R , E > > + MaybeSend + ' static ,
654656 A : FromLuaMulti ,
655657 R : IntoLuaMulti ,
658+ E : ExternalError ,
656659 {
657660 let func = RefCell :: new ( func) ;
658661 WrappedFunction ( Box :: new ( move |lua, nargs| unsafe {
659662 let mut func = func. try_borrow_mut ( ) . map_err ( |_| Error :: RecursiveMutCallback ) ?;
660663 let args = A :: from_stack_args ( nargs, 1 , None , lua) ?;
661- func. call ( args) ?. push_into_stack_multi ( lua)
664+ func. call ( args) . into_lua_err ( ) ?. push_into_stack_multi ( lua)
662665 } ) )
663666 }
664667
@@ -671,6 +674,7 @@ impl Function {
671674 pub fn wrap_raw < F , A > ( func : F ) -> impl IntoLua
672675 where
673676 F : LuaNativeFn < A > + MaybeSend + ' static ,
677+ F :: Output : IntoLuaMulti ,
674678 A : FromLuaMulti ,
675679 {
676680 WrappedFunction ( Box :: new ( move |lua, nargs| unsafe {
@@ -687,6 +691,7 @@ impl Function {
687691 pub fn wrap_raw_mut < F , A > ( func : F ) -> impl IntoLua
688692 where
689693 F : LuaNativeFnMut < A > + MaybeSend + ' static ,
694+ F :: Output : IntoLuaMulti ,
690695 A : FromLuaMulti ,
691696 {
692697 let func = RefCell :: new ( func) ;
@@ -701,11 +706,12 @@ impl Function {
701706 /// trait.
702707 #[ cfg( feature = "async" ) ]
703708 #[ cfg_attr( docsrs, doc( cfg( feature = "async" ) ) ) ]
704- pub fn wrap_async < F , A , R > ( func : F ) -> impl IntoLua
709+ pub fn wrap_async < F , A , R , E > ( func : F ) -> impl IntoLua
705710 where
706- F : LuaNativeAsyncFn < A , Output = Result < R > > + MaybeSend + ' static ,
711+ F : LuaNativeAsyncFn < A , Output = StdResult < R , E > > + MaybeSend + ' static ,
707712 A : FromLuaMulti ,
708713 R : IntoLuaMulti ,
714+ E : ExternalError ,
709715 {
710716 WrappedAsyncFunction ( Box :: new ( move |rawlua, nargs| unsafe {
711717 let args = match A :: from_stack_args ( nargs, 1 , None , rawlua) {
@@ -714,7 +720,7 @@ impl Function {
714720 } ;
715721 let lua = rawlua. lua ( ) ;
716722 let fut = func. call ( args) ;
717- Box :: pin ( async move { fut. await ?. push_into_stack_multi ( lua. raw_lua ( ) ) } )
723+ Box :: pin ( async move { fut. await . into_lua_err ( ) ?. push_into_stack_multi ( lua. raw_lua ( ) ) } )
718724 } ) )
719725 }
720726
@@ -728,6 +734,7 @@ impl Function {
728734 pub fn wrap_raw_async < F , A > ( func : F ) -> impl IntoLua
729735 where
730736 F : LuaNativeAsyncFn < A > + MaybeSend + ' static ,
737+ F :: Output : IntoLuaMulti ,
731738 A : FromLuaMulti ,
732739 {
733740 WrappedAsyncFunction ( Box :: new ( move |rawlua, nargs| unsafe {
@@ -789,22 +796,22 @@ impl<R: FromLuaMulti> Future for AsyncCallFuture<R> {
789796
790797/// A trait for types that can be used as Lua functions.
791798pub trait LuaNativeFn < A : FromLuaMulti > {
792- type Output : IntoLuaMulti ;
799+ type Output ;
793800
794801 fn call ( & self , args : A ) -> Self :: Output ;
795802}
796803
797804/// A trait for types with mutable state that can be used as Lua functions.
798805pub trait LuaNativeFnMut < A : FromLuaMulti > {
799- type Output : IntoLuaMulti ;
806+ type Output ;
800807
801808 fn call ( & mut self , args : A ) -> Self :: Output ;
802809}
803810
804811/// A trait for types that returns a future and can be used as Lua functions.
805812#[ cfg( feature = "async" ) ]
806813pub trait LuaNativeAsyncFn < A : FromLuaMulti > {
807- type Output : IntoLuaMulti ;
814+ type Output ;
808815
809816 fn call ( & self , args : A ) -> impl Future < Output = Self :: Output > + MaybeSend + ' static ;
810817}
@@ -815,7 +822,6 @@ macro_rules! impl_lua_native_fn {
815822 where
816823 FN : Fn ( $( $A, ) * ) -> R + MaybeSend + ' static ,
817824 ( $( $A, ) * ) : FromLuaMulti ,
818- R : IntoLuaMulti ,
819825 {
820826 type Output = R ;
821827
@@ -830,7 +836,6 @@ macro_rules! impl_lua_native_fn {
830836 where
831837 FN : FnMut ( $( $A, ) * ) -> R + MaybeSend + ' static ,
832838 ( $( $A, ) * ) : FromLuaMulti ,
833- R : IntoLuaMulti ,
834839 {
835840 type Output = R ;
836841
@@ -847,7 +852,6 @@ macro_rules! impl_lua_native_fn {
847852 FN : Fn ( $( $A, ) * ) -> Fut + MaybeSend + ' static ,
848853 ( $( $A, ) * ) : FromLuaMulti ,
849854 Fut : Future <Output = R > + MaybeSend + ' static ,
850- R : IntoLuaMulti ,
851855 {
852856 type Output = R ;
853857
0 commit comments