Skip to content

Storing lua values in rust #290

@GeTechG

Description

@GeTechG

Could you help with whether it is possible to safely store data from Lua. I have this test code.

struct LuaEngine<'a> {
    lua: Lua,
    world: Option<Table<'a>>,
}

impl LuaEngine<'_> {
    fn new() -> Self {
        LuaEngine {
            lua: Lua::new(),
            world: None,
        }
    }

    fn load_world(&self) -> Table {
        let chunk = self.lua.load("return { name = \"Mark\" }");
        let table = chunk.eval::<Table>().unwrap();
        table.clone()
    }

    fn ready(&mut self) {
        let table = self.load_world();
        self.world = Some(table);
    }
}

fn main() {
    let mut engine = LuaEngine::new();
    engine.ready();
    println!("{}", engine.world.unwrap().get::<_, mlua::String>("name").unwrap().to_str().unwrap())
}

I'm getting a time-of-life error.

error: lifetime may not live long enough
  --> src/main.rs:23:21
   |
22 |     fn ready(&mut self) {
   |              ---------
   |              |
   |              let's call the lifetime of this reference `'1`
   |              has type `&mut LuaEngine<'2>`
23 |         let table = self.load_world();
   |                     ^^^^^^^^^^^^^^^^^ argument requires that `'1` must outlive `'2`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions