如何从 Rust 中的上下文中检索 Lua 表格?
2019-7-26 15:25:36
收藏:0
阅读:104
评论:1
我正在尝试从 Rust 中的 lua 文件中检索表格。我正在注册一个 register_entity 函数到 lua 全局上下文中,以使数据文件能够注册它们的表格。当执行 lua 文件并调用 register_entity 函数时,在 Rust 中会调用已注册的回调函数。回调函数应该将传递的表格添加到 HashMap 中以维护所有实体的集合。
这是一个示例 lua 文件,正在读取它。
Goblin = {
glyph: "2"
}
register_entity("Goblin", Goblin)
Rust 代码
fn load_lua_globals(&self) {
let entities = Arc::new(Mutex::new(HashMap::new()));
self.lua.context(|lua_ctx| {
let register_entity = {
let entities = Arc::clone(&entities);
let register_entity = lua_ctx.create_function(
move |_, (name, table): (String, Table)| {
entities.lock().unwrap().insert(name, table);
Ok(())
}).unwrap();
};
lua_ctx.globals().set("register_entity", register_entity).unwrap();
});
}
}
这是错误。
error[E0277]: `*mut rlua::ffi::lua_State` cannot be sent between threads safely
--> src/bin/main.rs:106:47
|
106 | let register_entity = lua_ctx.create_function(
| ^^^^^^^^^^^^^^^ `*mut rlua::ffi::lua_State` cannot be sent between threads safely
|
= help: within `(std::string::String, rlua::Table<'_>)`, the trait `std::marker::Send` is not implemented for `*mut rlua::ffi::lua_State`
= note: required because it appears within the type `rlua::Context<'_>`
= note: required because it appears within the type `rlua::types::LuaRef<'_>`
= note: required because it appears within the type `rlua::Table<'_>`
= note: required because it appears within the type `(std::string::String, rlua::Table<'_>)`
= note: required because of the requirements on the impl of `std::marker::Send` for `hashbrown::raw::RawTable<(std::string::String, rlua::Table<'_>)>`
= note: required because it appears within the type `hashbrown::map::HashMap<std::string::String, rlua::Table<'_>, std::collections::hash_map::RandomState>`
= note: required because it appears within the type `std::collections::HashMap<std::string::String, rlua::Table<'_>>`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Mutex<std::collections::HashMap<std::string::String, rlua::Table<'_>>>`
= note: required because of the requirements on the impl of `std::marker::Send` for `std::sync::Arc<std::sync::Mutex<std::collections::HashMap<std::string::String, rlua::Table<'_>>>>`
= note: required because it appears within the type `[closure@src/bin/main.rs:107:21: 112:18 entities:std::sync::Arc<std::sync::Mutex<std::collections::HashMap<std::string::String, rlua::Table<'_>>>>]
点赞
评论区的留言会收到邮件通知哦~
推荐文章
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

你不能在
Lua::context之外存储对lua_ctx的引用。你在entities中存储的rlua::Table与lua_ctx的生命周期绑定。rlua提供了一种在上下文调用之外存储Lua对象引用的方法,即使用
Context::create_registry_value。它返回一个“Registry”键,您可以安全地将其存储在HashMap中,并在另一个Lua::context调用中使用它。请参阅以下代码以获得示例:use std::collections::HashMap; use std::sync::{Arc, Mutex}; use rlua::{Lua, RegistryKey, Table}; const LUA_SOURCE: &str = r#" Goblin = { glyph = "2" } register_entity("Goblin", Goblin) "#; fn setup_register_entity(lua: &Lua) -> Arc<Mutex<HashMap<String, RegistryKey>>> { let entities = Arc::new(Mutex::new(HashMap::new())); lua.context(|lua_ctx| { let entities = entities.clone(); let register_entity = lua_ctx.create_function( move |ctx, (name, table): (String, Table)| { // Store a refenrence to the object as a RegistryKey let key = ctx.create_registry_value(table) .expect("should have inserted in registry"); entities.lock().unwrap().insert(name, key); Ok(()) }) .unwrap(); lua_ctx.globals().set("register_entity", register_entity).unwrap(); }); entities } fn main() { let lua = Lua::new(); let entities = setup_register_entity(&lua); lua.context(|lua_ctx| { // Load lua code lua_ctx.load(LUA_SOURCE) .exec() .expect("should load"); for (name, key) in entities.lock().unwrap().iter() { // Retreive table let table: Table = lua_ctx.registry_value(key).unwrap(); // Use it let glyph: String = table.get("glyph").unwrap(); println!("{}.glyph = {}", name, glyph); } }); }