2017-08-01 13:55:08 -04:00
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
use std::iter::FromIterator;
|
2017-07-24 10:40:00 -04:00
|
|
|
use std::result::Result as StdResult;
|
2017-07-24 07:30:29 -04:00
|
|
|
|
2017-05-21 19:50:59 -04:00
|
|
|
use error::*;
|
|
|
|
use lua::*;
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
/// Result is convertible to `MultiValue` following the common lua idiom of returning the result
|
2017-06-27 14:05:49 -04:00
|
|
|
/// on success, or in the case of an error, returning nil followed by the error
|
2017-07-24 07:30:29 -04:00
|
|
|
impl<'lua, T: ToLua<'lua>, E: ToLua<'lua>> ToLuaMulti<'lua> for StdResult<T, E> {
|
2017-07-18 16:21:12 -04:00
|
|
|
fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
|
|
|
|
let mut result = MultiValue::new();
|
2017-06-13 19:17:41 -04:00
|
|
|
|
|
|
|
match self {
|
|
|
|
Ok(v) => result.push_back(v.to_lua(lua)?),
|
|
|
|
Err(e) => {
|
2017-07-24 07:12:52 -04:00
|
|
|
result.push_back(Nil);
|
2017-06-13 19:17:41 -04:00
|
|
|
result.push_back(e.to_lua(lua)?);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-21 19:50:59 -04:00
|
|
|
impl<'lua, T: ToLua<'lua>> ToLuaMulti<'lua> for T {
|
2017-07-18 16:21:12 -04:00
|
|
|
fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
|
|
|
|
let mut v = MultiValue::new();
|
2017-05-21 19:50:59 -04:00
|
|
|
v.push_back(self.to_lua(lua)?);
|
|
|
|
Ok(v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for T {
|
2017-07-18 16:21:12 -04:00
|
|
|
fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> {
|
2017-07-24 07:12:52 -04:00
|
|
|
Ok(T::from_lua(values.pop_front().unwrap_or(Nil), lua)?)
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
impl<'lua> ToLuaMulti<'lua> for MultiValue<'lua> {
|
|
|
|
fn to_lua_multi(self, _: &'lua Lua) -> Result<MultiValue<'lua>> {
|
2017-05-21 19:50:59 -04:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
impl<'lua> FromLuaMulti<'lua> for MultiValue<'lua> {
|
|
|
|
fn from_lua_multi(values: MultiValue<'lua>, _: &'lua Lua) -> Result<Self> {
|
2017-05-21 19:50:59 -04:00
|
|
|
Ok(values)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 14:05:49 -04:00
|
|
|
/// Can be used to pass variadic values to or receive variadic values from Lua, where the type of
|
|
|
|
/// the values is all the same and the number of values is defined at runtime. This can be included
|
2017-08-03 17:07:54 -04:00
|
|
|
/// in a tuple when converting from a MultiValue, but it must be the final entry, and it will
|
|
|
|
/// consume the rest of the parameters given.
|
2017-08-01 13:55:08 -04:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Variadic<T>(Vec<T>);
|
|
|
|
|
|
|
|
impl<T> Variadic<T> {
|
|
|
|
pub fn new() -> Variadic<T> {
|
|
|
|
Variadic(Vec::new())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> FromIterator<T> for Variadic<T> {
|
|
|
|
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
|
|
|
|
Variadic(Vec::from_iter(iter))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> IntoIterator for Variadic<T> {
|
|
|
|
type Item = T;
|
|
|
|
type IntoIter = <Vec<T> as IntoIterator>::IntoIter;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Deref for Variadic<T> {
|
|
|
|
type Target = Vec<T>;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> DerefMut for Variadic<T> {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
impl<'lua, T: ToLua<'lua>> ToLuaMulti<'lua> for Variadic<T> {
|
|
|
|
fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
|
2017-05-21 19:50:59 -04:00
|
|
|
self.0.into_iter().map(|e| e.to_lua(lua)).collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-18 16:21:12 -04:00
|
|
|
impl<'lua, T: FromLua<'lua>> FromLuaMulti<'lua> for Variadic<T> {
|
|
|
|
fn from_lua_multi(values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> {
|
2017-05-21 19:50:59 -04:00
|
|
|
values
|
|
|
|
.into_iter()
|
|
|
|
.map(|e| T::from_lua(e, lua))
|
2017-07-18 16:21:12 -04:00
|
|
|
.collect::<Result<Vec<T>>>()
|
|
|
|
.map(Variadic)
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
macro_rules! impl_tuple {
|
|
|
|
() => (
|
|
|
|
impl<'lua> ToLuaMulti<'lua> for () {
|
|
|
|
fn to_lua_multi(self, _: &'lua Lua) -> Result<MultiValue> {
|
|
|
|
Ok(MultiValue::new())
|
|
|
|
}
|
|
|
|
}
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
impl<'lua> FromLuaMulti<'lua> for () {
|
|
|
|
fn from_lua_multi(_: MultiValue, _: &'lua Lua) -> Result<Self> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
($last:ident $($name:ident)*) => (
|
|
|
|
impl<'lua, $($name,)* $last> ToLuaMulti<'lua> for ($($name,)* $last,)
|
|
|
|
where $($name: ToLua<'lua>,)*
|
|
|
|
$last: ToLuaMulti<'lua>
|
|
|
|
{
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>> {
|
|
|
|
let ($($name,)* $last,) = self;
|
|
|
|
|
|
|
|
let mut results = $last.to_lua_multi(lua)?;
|
|
|
|
push_reverse!(results, $($name.to_lua(lua)?,)*);
|
|
|
|
Ok(results)
|
|
|
|
}
|
|
|
|
}
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
impl<'lua, $($name,)* $last> FromLuaMulti<'lua> for ($($name,)* $last,)
|
|
|
|
where $($name: FromLua<'lua>,)*
|
|
|
|
$last: FromLuaMulti<'lua>
|
|
|
|
{
|
|
|
|
#[allow(unused_mut)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
fn from_lua_multi(mut values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self> {
|
|
|
|
$(let $name = values.pop_front().unwrap_or(Nil);)*
|
|
|
|
let $last = FromLuaMulti::from_lua_multi(values, lua)?;
|
|
|
|
Ok(($(FromLua::from_lua($name, lua)?,)* $last,))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
macro_rules! push_reverse {
|
|
|
|
($multi_value:expr, $first:expr, $($rest:expr,)*) => (
|
|
|
|
push_reverse!($multi_value, $($rest,)*);
|
|
|
|
$multi_value.push_front($first);
|
|
|
|
);
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
($multi_value:expr, $first:expr) => (
|
|
|
|
$multi_value.push_front($first);
|
|
|
|
);
|
2017-05-21 19:50:59 -04:00
|
|
|
|
2017-07-31 01:21:41 -04:00
|
|
|
($multi_value:expr,) => ();
|
2017-05-21 19:50:59 -04:00
|
|
|
}
|
2017-07-31 01:21:41 -04:00
|
|
|
|
2017-08-01 13:55:08 -04:00
|
|
|
impl_tuple!{}
|
2017-08-01 13:57:51 -04:00
|
|
|
impl_tuple!{A}
|
|
|
|
impl_tuple!{A B}
|
|
|
|
impl_tuple!{A B C}
|
|
|
|
impl_tuple!{A B C D}
|
|
|
|
impl_tuple!{A B C D E}
|
|
|
|
impl_tuple!{A B C D E F}
|
|
|
|
impl_tuple!{A B C D E F G}
|
|
|
|
impl_tuple!{A B C D E F G H}
|
|
|
|
impl_tuple!{A B C D E F G H I}
|
|
|
|
impl_tuple!{A B C D E F G H I J}
|
|
|
|
impl_tuple!{A B C D E F G H I J K}
|
|
|
|
impl_tuple!{A B C D E F G H I J K L}
|