luaext/types/
integer.rs

1use lua::{Index, ToLua, FromLua, State};
2use types::{LuaStackable};
3use context::Context;
4
5/// Represents an integer on the Lua Stack
6pub struct LuaInteger {
7    index: Index
8}
9
10impl LuaInteger {
11    /// Create a new LuaInteger given an index
12    pub fn new(i: Index) -> LuaInteger {
13        LuaInteger {
14            index: i
15        }
16    }
17
18    /// Get the value of this integer
19    pub fn get(&self, context: &mut Context) -> i64 {
20        context.get_state().to_integer(self.get_pos())
21    }
22}
23
24impl LuaStackable for LuaInteger {
25    fn get_pos(&self) -> Index {
26        self.index
27    }
28}
29
30impl ToLua for LuaInteger {
31    fn to_lua(&self, state: &mut State) {
32        state.push_value(self.get_pos());
33    }
34}
35
36impl FromLua for LuaInteger {
37    fn from_lua(state: &mut State, index: Index) -> Option<LuaInteger> {
38        if state.is_integer(index) {
39            Some(LuaInteger::new(index))
40        } else {
41            None
42        }
43    }
44}