1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::str;
use std::ops::{Deref, DerefMut};
use std::iter::FromIterator;
use std::collections::VecDeque;
use error::*;
use types::{Integer, LightUserData, Number};
use string::String;
use table::Table;
use function::Function;
use thread::Thread;
use userdata::AnyUserData;
use lua::Lua;
#[derive(Debug, Clone)]
pub enum Value<'lua> {
Nil,
Boolean(bool),
LightUserData(LightUserData),
Integer(Integer),
Number(Number),
String(String<'lua>),
Table(Table<'lua>),
Function(Function<'lua>),
Thread(Thread<'lua>),
UserData(AnyUserData<'lua>),
Error(Error),
}
pub use self::Value::Nil;
impl<'lua> Value<'lua> {
pub(crate) fn type_name(&self) -> &'static str {
match *self {
Value::Nil => "nil",
Value::Boolean(_) => "boolean",
Value::LightUserData(_) => "light userdata",
Value::Integer(_) => "integer",
Value::Number(_) => "number",
Value::String(_) => "string",
Value::Table(_) => "table",
Value::Function(_) => "function",
Value::Thread(_) => "thread",
Value::UserData(_) | Value::Error(_) => "userdata",
}
}
}
pub trait ToLua<'lua> {
fn to_lua(self, lua: &'lua Lua) -> Result<Value<'lua>>;
}
pub trait FromLua<'lua>: Sized {
fn from_lua(lua_value: Value<'lua>, lua: &'lua Lua) -> Result<Self>;
}
#[derive(Debug, Clone)]
pub struct MultiValue<'lua>(VecDeque<Value<'lua>>);
impl<'lua> MultiValue<'lua> {
pub fn new() -> MultiValue<'lua> {
MultiValue(VecDeque::new())
}
}
impl<'lua> FromIterator<Value<'lua>> for MultiValue<'lua> {
fn from_iter<I: IntoIterator<Item = Value<'lua>>>(iter: I) -> Self {
MultiValue(VecDeque::from_iter(iter))
}
}
impl<'lua> IntoIterator for MultiValue<'lua> {
type Item = Value<'lua>;
type IntoIter = <VecDeque<Value<'lua>> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'lua> Deref for MultiValue<'lua> {
type Target = VecDeque<Value<'lua>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<'lua> DerefMut for MultiValue<'lua> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
pub trait ToLuaMulti<'lua> {
fn to_lua_multi(self, lua: &'lua Lua) -> Result<MultiValue<'lua>>;
}
pub trait FromLuaMulti<'lua>: Sized {
fn from_lua_multi(values: MultiValue<'lua>, lua: &'lua Lua) -> Result<Self>;
}