我正试着用Mlua把Lua植入Rust.使用它的guided tour,我成功地加载并判断了一段Lua代码.我的下一个目标是将其与打开文件并读取其内容结合起来.这就是问题所在:
use anyhow::Result;
use mlua::{Lua, UserData};
use std::fs;
pub struct LuaManager {
lua_state: Lua
}
impl LuaManager {
pub fn new() -> Self {
LuaManager {
lua_state: Lua::new(),
}
}
pub fn eval<T>(&self, chunk: &str) -> Result<T> where T: Clone + UserData {
let v = self.lua_state.load(chunk).eval::<T>()?;
Ok(v)
}
}
因为脚本可以返回任何东西,所以我认为特性锁定的泛型注释可以解决这个问题:问题是:
Error generated by rustc: the parameter type `T` may not live long enough... so that the type `T` will meet its required lifetime bounds...
以下是lua中定义的函数签名,仅供参考.rs:
pub fn eval<R: FromLuaMulti<'lua>>(self) -> Result<R>
我不明白我为什么会有这个问题.我已经查过了,这个问题与用户创建的类型有关,据我所知,我没有这样做...那么这里的问题是什么?很明显,像<i32>
这样的东西不会很快消失.Cargo确实建议添加一个静态的生命周期注释,但对它进行一些研究后,我会try 在不首先使用它的情况下解决这个问题,因为在99%的情况下,你实际上可以不使用它.