|
In mlua it is possible to yield from an use std::{time::{Duration, Instant}};
use futures_util::TryStreamExt;
use mlua::{Lua, UserData, chunk};
#[derive(Default)]
struct MyUserData {}
impl UserData for MyUserData {
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
methods.add_async_method("wait_ms", async |lua, _, milliseconds: u64| {
let end_time = Instant::now() + Duration::from_millis(milliseconds);
while Instant::now() < end_time {
lua.yield_with::<()>(()).await?;
}
Ok(())
});
}
}
#[tokio::main]
async fn main() {
let lua = Lua::new();
lua.globals()
.set("custom_data", MyUserData::default())
.unwrap();
print!("Starting Lua code execution...\n");
let function = lua
.load(chunk! {
print("Lua code started.")
custom_data:wait_ms(2000)
print("Lua code finished after waiting.")
})
.into_function()
.unwrap();
let mut threads = vec![];
for _ in 0..100 {
let thread = lua
.create_thread(function.clone())
.unwrap()
.into_async::<()>(())
.unwrap();
threads.push(thread);
}
loop {
let mut all_done = true;
for thread in &mut threads {
if thread.try_next().await.unwrap().is_some() {
all_done = false;
}
}
if all_done {
break;
}
}
}At the first try, this code works without any problems. However as soon as i enable the called `Result::unwrap()` on an `Err` value: CallbackError { traceback: "stack traceback:\n\t[C]: in ?\n\t__mlua_async_poll:4: in function <__mlua_async_poll:1>\n\tmlua-test/src/main.rs:35:1: in function <mlua-test/src/main.rs:35:1>", cause: BadArgument { to: Some("MyUserData.wait_ms"), pos: 1, name: Some("self"), cause: UserDataBorrowError } }In this example, the yield is not necessary, however this is only for demonstration purposes. I've read in the docs: |
Answered by
khvzak
Feb 28, 2026
Replies: 1 comment 1 reply
|
There were some breaking changes in the recent Rust compiler (1.93+). It should be fixed in the latest |
1 reply
Answer selected by
se7kn8
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There were some breaking changes in the recent Rust compiler (1.93+). It should be fixed in the latest
mainbranch. Could you try?