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

Trait for interacting with the Executor for the current Scheduler.

Provides extra methods on the Lua struct for:

  • Spawning thread-local (!Send) futures on the current executor
  • Spawning background (Send) futures on the current executor
  • Spawning blocking tasks on a separate thread pool

Required Methods§

source

fn spawn<F, T>(&self, fut: F) -> Task<T>
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

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(())
}
source

fn spawn_local<F>(&self, fut: F)
where F: Future<Output = ()> + 'static,

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(())
}
source

fn spawn_blocking<F, T>(&self, f: F) -> Task<T>
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

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.

Implementations on Foreign Types§

source§

impl<'lua> LuaSpawnExt<'lua> for Lua

source§

fn spawn<F, T>(&self, fut: F) -> Task<T>
where F: Future<Output = T> + Send + 'static, T: Send + 'static,

source§

fn spawn_local<F>(&self, fut: F)
where F: Future<Output = ()> + 'static,

source§

fn spawn_blocking<F, T>(&self, f: F) -> Task<T>
where F: FnOnce() -> T + Send + 'static, T: Send + 'static,

Implementors§