luaext/types/
userdata.rs

1use lua::{Index, ToLua, FromLua, State};
2use types::{LuaStackable, LuaTable};
3use context::Context;
4
5/// Represents user-defined data on the Lua stack
6///
7/// Note that without a __gc metamethod, any data contained in the userdata that implements the
8/// Drop trait will not be dropped, which although not unsafe, may result in memory leaks.
9/// This includes types such as HashMap and Vec.
10pub struct LuaUserdata {
11    index: Index
12}
13
14impl LuaUserdata {
15    /// Create a new LuaUserdata at the given index
16    pub fn new(i: Index) -> LuaUserdata {
17        LuaUserdata {
18            index: i
19        }
20    }
21
22    /// Set this userdata's metatable.
23    pub fn set_metatable(&self, context: &mut Context, meta: &LuaTable) {
24        context.get_state().push_value(meta.get_pos());
25        context.get_state().set_metatable(self.get_pos());
26    }
27
28    /// Get a mutable reference to this userdata's contained data.
29    pub unsafe fn get_value<'a, T>(&self, context: &'a mut Context) -> Option<&'a mut T> {
30        context.get_state().to_userdata_typed(self.index)
31    }
32
33    /// Get a mutable reference to this userdata's contained data, given that its metatable
34    /// matches the given name.
35    pub unsafe fn get_value_named<'a, T>(&self, context: &'a mut Context, name: &str)
36            -> Option<&'a mut T> {
37        context.get_state().test_userdata_typed(self.index, name)
38    }
39}
40
41impl LuaStackable for LuaUserdata {
42    fn get_pos(&self) -> Index {
43        self.index
44    }
45}
46
47impl ToLua for LuaUserdata {
48    fn to_lua(&self, state: &mut State) {
49        state.push_value(self.get_pos());
50    }
51}
52
53impl FromLua for LuaUserdata {
54    fn from_lua(state: &mut State, index: Index) -> Option<LuaUserdata> {
55        if state.is_userdata(index) {
56            Some(LuaUserdata::new(index))
57        } else {
58            None
59        }
60    }
61}