Skip to content

Instantly share code, notes, and snippets.

@singalen
Last active November 4, 2021 00:22
Show Gist options
  • Save singalen/fd45a5176dcc81fd1fa50a836256128d to your computer and use it in GitHub Desktop.
Save singalen/fd45a5176dcc81fd1fa50a836256128d to your computer and use it in GitHub Desktop.
Newbie lifetime problem
use rlua::{Context, Error, FromLua, Lua, Value};
fn eval2<'lua, T>(lua_text: &str) -> rlua::Result<T> // line 190
where T: FromLua<'lua>
{
let lua = Lua::new();
let result = lua.context(|lua_ctx: Context<'lua>| {
let result = lua_ctx
.load(lua_text)
.eval::<T>()
;
result
})?;
Ok(result)
}
error[E0308]: mismatched types
--> depends/step-talk/src/eval.rs:190:40
|
190 | let result = lua.context(|lua_ctx: Context<'lua>| {
| ^^^^^^^^^^^^^ lifetime mismatch
|
= note: expected struct `LuaContext<'_>`
found struct `LuaContext<'lua>`
note: the anonymous lifetime #1 defined on the body at 190:30...
--> depends/step-talk/src/eval.rs:190:30
|
190 | let result = lua.context(|lua_ctx: Context<'lua>| {
| ______________________________^
191 | |
192 | | let result = lua_ctx
193 | | .load(lua_text)
... |
197 | | result
198 | | })?;
| |_____^
note: ...does not necessarily outlive the lifetime `'lua` as defined on the function body at 185:10
--> depends/step-talk/src/eval.rs:185:10
|
185 | fn eval2<'lua, T>(lua_text: &str) -> rlua::Result<T>
| ^^^^
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'lua` due to conflicting requirements
--> depends/step-talk/src/eval.rs:193:14
|
193 | .load(lua_text)
| ^^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the body at 190:30...
--> depends/step-talk/src/eval.rs:190:30
|
190 | let result = lua.context(|lua_ctx: Context<'lua>| {
| ______________________________^
191 | |
192 | | let result = lua_ctx
193 | | .load(lua_text)
... |
197 | | result
198 | | })?;
| |_____^
note: ...so that the types are compatible
--> depends/step-talk/src/eval.rs:193:14
|
193 | .load(lua_text)
| ^^^^
= note: expected `LuaContext<'_>`
found `LuaContext<'_>`
note: but, the lifetime must be valid for the lifetime `'lua` as defined on the function body at 185:10...
--> depends/step-talk/src/eval.rs:185:10
|
185 | fn eval2<'lua, T>(lua_text: &str) -> rlua::Result<T>
| ^^^^
note: ...so that the types are compatible
--> depends/step-talk/src/eval.rs:194:14
|
194 | .eval::<T>()
| ^^^^
= note: expected `FromLua<'_>`
found `FromLua<'lua>`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment