typwire
Typed data exchange between Typst and (Rust) WASM plugins.
Installation
Add the Rust crate to a plugin:
Add the Typst package to a document:
#import "@preview/typwire:0.2.0"
Quick Start
Export a plugin function from Rust:
use ;
use ;
initiate_protocol!;
Call the plugin function from Typst:
#import "@preview/typwire:0.2.0"
#let plugin = plugin("plugin.wasm")
#let report = typwire.call(
plugin.measure,
42,
(label: "Value: ", scale: 1.5),
12pt,
(source: "example", enabled: true),
)
#assert(report.text == "Value: 42")
#assert(report.width == 12pt)
Performance
If you're considering moving a pure-Typst implementation to a WASM plugin via typwire, be aware of the performance overhead that comes with it. These measurements were taken with medium-sized data, like a gradient with 9 stops; performance scales directly with the size of the data.
- ~10 ms to load the plugin, once per compilation.
- ~1 ms extra for the first call.
- ~150 μs per call after that.
Use CBOR unless you specifically need JSON, which is ~2.5x slower. Compared to sertyp, typwire is ~2x to ~66x faster, depending on the workload. See the full benchmark results.
Version compatibility
The Rust crate and the Typst package are versioned together and must be on the exact same version. There is no compatibility guarantee across a version mismatch.
Documentation
The Typst API reference can be found in the project repository.
License
typwire is available under the MIT OR Apache-2.0 license. See
LICENSE-MIT and
LICENSE-APACHE
for details.