luaext/types/
boolean.rs

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