Skip to content
Axiom/SDK
SDK / Mods in Rust

Mods in Rust

A Rust mod is a no_std library compiled for wasm32-unknown-unknown, which calls the surface through axiom.rs: the structs, event constants and imports from surface.h, written as Rust and generated from the same header, so its calls are the C calls under the same names.

Set up

Terminal
$ rustup target add wasm32-unknown-unknown
$ cargo new --lib hello
$ curl -o hello/src/axiom.rs https://www.axiommods.com/sdk/axiom.rs

Use wasm32-unknown-unknown, not wasm32-wasip1: a WASI module imports wasi_snapshot_preview1, which the surface does not offer, and is refused when it loads.

The crate

CARGO.TOML
[package]
name = "hello"
version = "0.1.0"
edition = "2024"

[lib]
crate-type = ["cdylib"]

[profile.release]
opt-level = "s"
lto = true
panic = "abort"
strip = true

cdylib builds a module with exports, and panic = "abort" keeps the unwinding machinery out of it.

SRC/LIB.RSa panel, a graph and a button
#![no_std]

mod axiom;

use axiom::*;

/// A string crosses as a pointer and a length in bytes.
fn text(panel: AxiomUiOverlayPanelId, line: &str) {
    unsafe {
        axiom_ui_overlay_text(panel.index, panel.generation, line.as_ptr(), line.len() as u32);
    }
}

static mut PRESSES: u32 = 0;
static mut PAYLOAD: [u8; 128] = [0; 128];

#[unsafe(no_mangle)]
pub extern "C" fn on_frame() {
    let mut panel = AxiomUiOverlayPanelId::default();
    if unsafe { axiom_ui_overlay_panel("Hello".as_ptr(), 5, &mut panel) } != 0 {
        return;
    }
    text(panel, "Drawn by a Rust mod.");

    let ms = unsafe { axiom_frame_delta() } * 1000.0;
    let label = "Frame time (ms)";
    unsafe {
        axiom_ui_overlay_graph(panel.index, panel.generation, label.as_ptr(), label.len() as u32, ms, 120);
        axiom_ui_overlay_button(panel.index, panel.generation, "Reset".as_ptr(), 5, "reset".as_ptr(), 5);
    }
}

#[unsafe(no_mangle)]
pub extern "C" fn on_event(id: u32, size: u32) {
    let payload = unsafe { &mut *core::ptr::addr_of_mut!(PAYLOAD) };
    if size as usize > payload.len()
        || unsafe { axiom_event_read(payload.as_mut_ptr(), payload.len() as u32) } != AXIOM_EVENT_READ_OK
    {
        return;
    }
    if id == AXIOM_EVENT_UI_OVERLAY_PRESSED {
        unsafe { PRESSES += 1 };
    }
}

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
    core::arch::wasm32::unreachable()
}
  • Exports are #[unsafe(no_mangle)] pub extern "C" fn on_frame() and, for events, on_event(id: u32, size: u32). On edition 2021, write #[no_mangle].
  • Every surface call is unsafe, as any foreign function is. Wrap the ones you use in small safe functions, as text does above, and keep unsafe at that edge.
  • Strings cross as as_ptr() and len(), in bytes. A Rust &str is always UTF-8, which is what the host requires.
  • A panic handler is required by no_std. Trapping with unreachable is a fault the host counts, which is the right answer to a panic.

Build

Terminal
$ cargo build --release --target wasm32-unknown-unknown
$ cp target/wasm32-unknown-unknown/release/hello.wasm mod.wasm
$ axiom check
$ axiom build

The module from the example above is under 600 bytes, and its imports are exactly the five calls it makes plus axiom_event_read.

Out pointers and statuses

A call that returns a value through an out pointer takes &mut to a value of yours, and returns a status:

OUT POINTERS
let mut position = AxiomVec3::default();
let status = unsafe {
    axiom_entity_transform_position(id.index, id.generation, &mut position)
};
if status != 0 {
    // 1 is StaleHandle; position was not written.
}

The structs derive Default, so a zeroed one is a line. Bulk calls take a pointer to the start of a mutable array and its length, and return how many they wrote.

What to avoid

  • alloc without an allocator. Vec and String need a global allocator, and none is set up for you. Keep state in static arrays, or bring a small allocator and declare it with #[global_allocator]: a mod may grow its own memory up to 64 MiB.
  • std. It assumes an operating system the sandbox does not give.
  • Holding handles in a static across frames. Ask for them each frame (Handles).

Why a generated file and not a crate

The Axiom repository's own Rust output does not follow the guest ABI yet, so there is no crate to depend on. axiom.rs is written from the C header the docs serve, every import checked against the surface's import list, and changes when the surface does. Download it again after a surface change.