use crate::error::Result;
use crate::multi::MultiValue;
use crate::state::Lua;
use crate::value::Value;
pub trait IntoLua: Sized {
fn into_lua(self, lua: &Lua) -> Result<Value>;
}
pub trait FromLua: Sized {
fn from_lua(value: Value, lua: &Lua) -> Result<Self>;
fn from_lua_arg(arg: Value, _i: usize, _to: Option<&str>, lua: &Lua) -> Result<Self> {
Self::from_lua(arg, lua)
}
}
pub trait IntoLuaMulti: Sized {
fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue>;
}
pub trait FromLuaMulti: Sized {
fn from_lua_multi(values: MultiValue, lua: &Lua) -> Result<Self>;
}
impl<T: IntoLua> IntoLuaMulti for T {
fn into_lua_multi(self, lua: &Lua) -> Result<MultiValue> {
let mut m = MultiValue::with_capacity(1);
m.push_back(self.into_lua(lua)?);
Ok(m)
}
}
impl<T: FromLua> FromLuaMulti for T {
fn from_lua_multi(mut values: MultiValue, lua: &Lua) -> Result<Self> {
let v = values.pop_front().unwrap_or(Value::Nil);
T::from_lua(v, lua)
}
}