Skip to content

Commit d64d971

Browse files
committed
Replace Either enum with implementation from either crate
1 parent 75475fc commit d64d971

File tree

4 files changed

+58
-137
lines changed

4 files changed

+58
-137
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ anyhow = ["dep:anyhow"]
4646
[dependencies]
4747
mlua_derive = { version = "=0.10.0-rc.1", optional = true, path = "mlua_derive" }
4848
bstr = { version = "1.0", features = ["std"], default-features = false }
49+
either = "1.0"
4950
num-traits = { version = "0.2.14" }
5051
rustc-hash = "2.0"
5152
futures-util = { version = "0.3", optional = true, default-features = false, features = ["std"] }

src/conversion.rs

+57-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::string::String;
1717
use crate::table::Table;
1818
use crate::thread::Thread;
1919
use crate::traits::{FromLua, IntoLua, ShortTypeName as _};
20-
use crate::types::{LightUserData, MaybeSend, RegistryKey};
20+
use crate::types::{Either, LightUserData, MaybeSend, RegistryKey};
2121
use crate::userdata::{AnyUserData, UserData};
2222
use 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+
}

src/types.rs

-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ impl LuaType for LightUserData {
123123
}
124124

125125
mod app_data;
126-
mod either;
127126
mod registry_key;
128127
mod sync;
129128
mod value_ref;

src/types/either.rs

-135
This file was deleted.

0 commit comments

Comments
 (0)