Skip to content

Commit

Permalink
Replace Either enum with implementation from either crate
Browse files Browse the repository at this point in the history
  • Loading branch information
khvzak committed Oct 20, 2024
1 parent 75475fc commit d64d971
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 137 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ anyhow = ["dep:anyhow"]
[dependencies]
mlua_derive = { version = "=0.10.0-rc.1", optional = true, path = "mlua_derive" }
bstr = { version = "1.0", features = ["std"], default-features = false }
either = "1.0"
num-traits = { version = "0.2.14" }
rustc-hash = "2.0"
futures-util = { version = "0.3", optional = true, default-features = false, features = ["std"] }
Expand Down
58 changes: 57 additions & 1 deletion src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::string::String;
use crate::table::Table;
use crate::thread::Thread;
use crate::traits::{FromLua, IntoLua, ShortTypeName as _};
use crate::types::{LightUserData, MaybeSend, RegistryKey};
use crate::types::{Either, LightUserData, MaybeSend, RegistryKey};
use crate::userdata::{AnyUserData, UserData};
use crate::value::{Nil, Value};

Expand Down Expand Up @@ -1039,3 +1039,59 @@ impl<T: FromLua> FromLua for Option<T> {
}
}
}

impl<L: IntoLua, R: IntoLua> IntoLua for Either<L, R> {
#[inline]
fn into_lua(self, lua: &Lua) -> Result<Value> {
match self {
Either::Left(l) => l.into_lua(lua),
Either::Right(r) => r.into_lua(lua),
}
}

#[inline]
unsafe fn push_into_stack(self, lua: &RawLua) -> Result<()> {
match self {
Either::Left(l) => l.push_into_stack(lua),
Either::Right(r) => r.push_into_stack(lua),
}
}
}

impl<L: FromLua, R: FromLua> FromLua for Either<L, R> {
#[inline]
fn from_lua(value: Value, lua: &Lua) -> Result<Self> {
let value_type_name = value.type_name();
// Try the left type first
match L::from_lua(value.clone(), lua) {
Ok(l) => Ok(Either::Left(l)),
// Try the right type
Err(_) => match R::from_lua(value, lua).map(Either::Right) {
Ok(r) => Ok(r),
Err(_) => Err(Error::FromLuaConversionError {
from: value_type_name,
to: Self::type_name(),
message: None,
}),
},
}
}

#[inline]
unsafe fn from_stack(idx: c_int, lua: &RawLua) -> Result<Self> {
match L::from_stack(idx, lua) {
Ok(l) => Ok(Either::Left(l)),
Err(_) => match R::from_stack(idx, lua).map(Either::Right) {
Ok(r) => Ok(r),
Err(_) => {
let value_type_name = CStr::from_ptr(ffi::luaL_typename(lua.state(), idx));
Err(Error::FromLuaConversionError {
from: value_type_name.to_str().unwrap(),
to: Self::type_name(),
message: None,
})
}
},
}
}
}
1 change: 0 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ impl LuaType for LightUserData {
}

mod app_data;
mod either;
mod registry_key;
mod sync;
mod value_ref;
Expand Down
135 changes: 0 additions & 135 deletions src/types/either.rs

This file was deleted.

0 comments on commit d64d971

Please sign in to comment.