luaext/types/
string.rs

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