@@ -17,7 +17,7 @@ use crate::string::String;
1717use crate :: table:: Table ;
1818use crate :: thread:: Thread ;
1919use crate :: traits:: { FromLua , IntoLua , ShortTypeName as _} ;
20- use crate :: types:: { LightUserData , MaybeSend , RegistryKey } ;
20+ use crate :: types:: { Either , LightUserData , MaybeSend , RegistryKey } ;
2121use crate :: userdata:: { AnyUserData , UserData } ;
2222use crate :: value:: { Nil , Value } ;
2323
@@ -1039,3 +1039,59 @@ impl<T: FromLua> FromLua for Option<T> {
10391039 }
10401040 }
10411041}
1042+
1043+ impl < L : IntoLua , R : IntoLua > IntoLua for Either < L , R > {
1044+ #[ inline]
1045+ fn into_lua ( self , lua : & Lua ) -> Result < Value > {
1046+ match self {
1047+ Either :: Left ( l) => l. into_lua ( lua) ,
1048+ Either :: Right ( r) => r. into_lua ( lua) ,
1049+ }
1050+ }
1051+
1052+ #[ inline]
1053+ unsafe fn push_into_stack ( self , lua : & RawLua ) -> Result < ( ) > {
1054+ match self {
1055+ Either :: Left ( l) => l. push_into_stack ( lua) ,
1056+ Either :: Right ( r) => r. push_into_stack ( lua) ,
1057+ }
1058+ }
1059+ }
1060+
1061+ impl < L : FromLua , R : FromLua > FromLua for Either < L , R > {
1062+ #[ inline]
1063+ fn from_lua ( value : Value , lua : & Lua ) -> Result < Self > {
1064+ let value_type_name = value. type_name ( ) ;
1065+ // Try the left type first
1066+ match L :: from_lua ( value. clone ( ) , lua) {
1067+ Ok ( l) => Ok ( Either :: Left ( l) ) ,
1068+ // Try the right type
1069+ Err ( _) => match R :: from_lua ( value, lua) . map ( Either :: Right ) {
1070+ Ok ( r) => Ok ( r) ,
1071+ Err ( _) => Err ( Error :: FromLuaConversionError {
1072+ from : value_type_name,
1073+ to : Self :: type_name ( ) ,
1074+ message : None ,
1075+ } ) ,
1076+ } ,
1077+ }
1078+ }
1079+
1080+ #[ inline]
1081+ unsafe fn from_stack ( idx : c_int , lua : & RawLua ) -> Result < Self > {
1082+ match L :: from_stack ( idx, lua) {
1083+ Ok ( l) => Ok ( Either :: Left ( l) ) ,
1084+ Err ( _) => match R :: from_stack ( idx, lua) . map ( Either :: Right ) {
1085+ Ok ( r) => Ok ( r) ,
1086+ Err ( _) => {
1087+ let value_type_name = CStr :: from_ptr ( ffi:: luaL_typename ( lua. state ( ) , idx) ) ;
1088+ Err ( Error :: FromLuaConversionError {
1089+ from : value_type_name. to_str ( ) . unwrap ( ) ,
1090+ to : Self :: type_name ( ) ,
1091+ message : None ,
1092+ } )
1093+ }
1094+ } ,
1095+ }
1096+ }
1097+ }
0 commit comments