Replies: 2 comments 4 replies
-
You need to make
static LUA: Lazy<parking_lot::Mutex<Lua>> = Lazy::new(|| parking_lot::Mutex::new(Lua::new()));
struct MyStruct {
func: RegistryKey,
}
fn main() -> Result<()> {
// To create
let my_struct = {
let lua = LUA.lock();
let func = lua.create_function(|_, ()| {
println!("hello");
Ok(())
})?;
MyStruct {
func: lua.create_registry_value(func)?,
}
};
// To call
{
let lua = LUA.lock();
let func = lua.registry_value::<Function>(&my_struct.func)?;
func.call(())?;
}
Ok(())
} |
Beta Was this translation helpful? Give feedback.
2 replies
-
It seems doesn't work, here's my codes (with use mlua::{Function, Lua, RegistryKey};
use once_cell::sync::Lazy;
static LUA: Lazy<parking_lot::Mutex<Lua>> = Lazy::new(|| parking_lot::Mutex::new(Lua::new()));
struct MyStruct {
func: RegistryKey,
}
fn main() -> anyhow::Result<()> {
// To create
let my_struct = {
let lua = LUA.lock();
let func = lua.create_function(|_, ()| {
println!("hello");
Ok(())
})?;
MyStruct {
func: lua.create_registry_value(func)?,
}
};
// To call
{
let lua = LUA.lock();
let func = lua.registry_value::<Function>(&my_struct.func)?;
func.call(())?;
}
Ok(())
} |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a struct that needs to contain some lua callback functions. That struct represents a global state defined in a
lazy_static
, behind aRwLock
. I've tried it with bothstd::sync::RwLock
andparking_lot::RwLock
(with the adequate feature flag enabled), but I keep getting the same errors:Is this an issue specific to lazy_static? Is there a way to accomplish what I want? And yes, I do have the
send
feature flag enabled.Beta Was this translation helpful? Give feedback.
All reactions