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
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
[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.
#![no_std] mod axiom; axiom::*; /// A string crosses as a pointer and a length in bytes. text(panel: AxiomUiOverlayPanelId, line: &str) { unsafe { axiom_ui_overlay_text(panel.index, panel.generation, line.as_ptr(), line.len() u32); } } static PRESSES: u32 = 0; static PAYLOAD: [u8; 128] = [0; 128]; #[unsafe(no_mangle)] extern "C" on_frame() { panel = AxiomUiOverlayPanelId::default(); unsafe { axiom_ui_overlay_panel("Hello".as_ptr(), 5, & panel) } != 0 { ; } text(panel, "Drawn by a Rust mod."); ms = unsafe { axiom_frame_delta() } * 1000.0; label = "Frame time (ms)"; unsafe { axiom_ui_overlay_graph(panel.index, panel.generation, label.as_ptr(), label.len() u32, ms, 120); axiom_ui_overlay_button(panel.index, panel.generation, "Reset".as_ptr(), 5, "reset".as_ptr(), 5); } } #[unsafe(no_mangle)] extern "C" on_event(id: u32, size: u32) { payload = unsafe { & *core::ptr::addr_of_mut!(PAYLOAD) }; size usize > payload.len() || unsafe { axiom_event_read(payload.as_mut_ptr(), payload.len() u32) } != AXIOM_EVENT_READ_OK { ; } id == AXIOM_EVENT_UI_OVERLAY_PRESSED { unsafe { PRESSES += 1 }; } } #[panic_handler] 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, astextdoes above, and keepunsafeat that edge. - Strings cross as
as_ptr()andlen(), in bytes. A Rust&stris always UTF-8, which is what the host requires. - A panic handler is required by
no_std. Trapping withunreachableis a fault the host counts, which is the right answer to a panic.
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:
position = AxiomVec3::default(); status = unsafe { axiom_entity_transform_position(id.index, id.generation, & position) }; 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
allocwithout an allocator.VecandStringneed a global allocator, and none is set up for you. Keep state instaticarrays, 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
staticacross 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.