Skip to main content

Module helpers

Module helpers 

Source
Expand description

Glue between Swan promises and the host’s async runtime.

The engine runs imports as background tasks. It does not depend on any runtime: the host passes a SpawnFn that starts a Task on its own executor. Without one, tasks go onto a thread-local queue that the host drains with run_pending.

promise_async is also useful to hosts: it turns a Rust future into a Swan promise, so a host function can do asynchronous work.

§Examples

A host function that returns a promise:

use swan_core::value::{BuiltinFn, Value};
use texl_core::{Engine, helpers::promise_async};

let engine = Engine::new(None);
engine.add_builtin("fetch_answer", Value::Builtin(BuiltinFn::new("fetch_answer", |_| {
    promise_async(None, async { Value::Numb(42.0) })
})));
assert_eq!(block_on(engine.render("<% fetch_answer() %>")), "42");

Functions§

promise_async
Wraps an async operation in a background task that resolves a Swan Promise.
run_pending
Polls queued fallback tasks until no further progress is possible.
spawn
Runs task with spawner, or queues it with spawn_default if spawner is None.
spawn_default
Fallback spawner: queues the task on a thread-local queue.
spawner_fn
Builds a SpawnFn from a plain closure.

Type Aliases§

SpawnFn
A host-provided function that runs a background task to completion.
Task
A background task, as handed to a spawner. It is not Send: tasks run on the thread that created them.