Trait mlua_luau_scheduler::LuaSpawnExt
source · pub trait LuaSpawnExt<'lua> {
// Required methods
fn spawn<F, T>(&self, fut: F) -> Task<T>
where F: Future<Output = T> + Send + 'static,
T: Send + 'static;
fn spawn_local<F>(&self, fut: F)
where F: Future<Output = ()> + 'static;
fn spawn_blocking<F, T>(&self, f: F) -> Task<T>
where F: FnOnce() -> T + Send + 'static,
T: Send + 'static;
}
Expand description
Required Methods§
sourcefn spawn<F, T>(&self, fut: F) -> Task<T>
fn spawn<F, T>(&self, fut: F) -> Task<T>
Spawns the given future on the current executor and returns its Task
.
§Panics
Panics if called outside of a running Scheduler
.
§Example usage
use async_io::block_on;
use mlua::prelude::*;
use mlua_luau_scheduler::*;
fn main() -> LuaResult<()> {
let lua = Lua::new();
lua.globals().set(
"spawnBackgroundTask",
lua.create_async_function(|lua, ()| async move {
lua.spawn(async move {
println!("Hello from background task!");
}).await;
Ok(())
})?
)?;
let sched = Scheduler::new(&lua);
sched.push_thread_front(lua.load("spawnBackgroundTask()"), ());
block_on(sched.run());
Ok(())
}
sourcefn spawn_local<F>(&self, fut: F)
fn spawn_local<F>(&self, fut: F)
Spawns the given thread-local future on the current executor.
Note that this future will run detached and always to completion,
preventing the Scheduler
was spawned on from completing until done.
§Panics
Panics if called outside of a running Scheduler
.
§Example usage
use async_io::block_on;
use mlua::prelude::*;
use mlua_luau_scheduler::*;
fn main() -> LuaResult<()> {
let lua = Lua::new();
lua.globals().set(
"spawnLocalTask",
lua.create_async_function(|lua, ()| async move {
lua.spawn_local(async move {
println!("Hello from local task!");
});
Ok(())
})?
)?;
let sched = Scheduler::new(&lua);
sched.push_thread_front(lua.load("spawnLocalTask()"), ());
block_on(sched.run());
Ok(())
}
sourcefn spawn_blocking<F, T>(&self, f: F) -> Task<T>
fn spawn_blocking<F, T>(&self, f: F) -> Task<T>
Spawns the given blocking function and returns its Task
.
This function will run on a separate thread pool and not block the current executor.
§Panics
Panics if called outside of a running Scheduler
.
§Example usage
use async_io::block_on;
use mlua::prelude::*;
use mlua_luau_scheduler::*;
fn main() -> LuaResult<()> {
let lua = Lua::new();
lua.globals().set(
"spawnBlockingTask",
lua.create_async_function(|lua, ()| async move {
lua.spawn_blocking(|| {
println!("Hello from blocking task!");
}).await;
Ok(())
})?
)?;
let sched = Scheduler::new(&lua);
sched.push_thread_front(lua.load("spawnBlockingTask()"), ());
block_on(sched.run());
Ok(())
}
Object Safety§
This trait is not object safe.