luaext/types/
thread.rs

1use lua::{Index, ToLua, FromLua, State};
2use types::LuaStackable;
3use context::Context;
4
5/// Represents a Lua thread on the Lua stack
6pub struct LuaThread {
7    index: Index
8}
9
10impl LuaThread {
11    /// Create a new LuaThread at the given index
12    pub fn new(i: Index) -> LuaThread {
13        LuaThread {
14            index: i
15        }
16    }
17
18    /// Get the contained State
19    pub fn as_state(&self, context: &mut Context) -> State {
20        // Unwrapping here is safe because LuaThread is guaranteed to refer to a State
21        context.get_state().to_thread(self.index).unwrap()
22    }
23}
24
25impl LuaStackable for LuaThread {
26    fn get_pos(&self) -> Index {
27        self.index
28    }
29}
30
31impl ToLua for LuaThread {
32    fn to_lua(&self, state: &mut State) {
33        state.push_value(self.get_pos());
34    }
35}
36
37impl FromLua for LuaThread {
38    fn from_lua(state: &mut State, index: Index) -> Option<LuaThread> {
39        if state.is_thread(index) {
40            Some(LuaThread::new(index))
41        } else {
42            None
43        }
44    }
45}