xy โ€” Rust bindings for libxylem

Ergonomic Rust bindings for libxylem, a C library for hook-based extensibility and dynamic module loading.

Modules (shared libraries) implement typed hooks; the host dispatches to all loaded modules that provide a given hook. Modules can load other modules, call other hooks, define new hooks, and manage isolated region namespaces.


Crates

CrateDescription
xyErgonomic facade โ€” FFI types, safe wrappers, macros, prelude. This is what users add as a dependency.
xy-macrosProc-macro crate (#[xy_impl], #[xy_decl], #[xy_def], etc.). Used automatically via xy.

Prerequisites

The Rust crates link against libxylem. Install it before building:

# See https://github.com/tty-pt/ci/blob/main/docs/install.md#install-ttypt-packages
# and use `libxylem` as the package name.

libxylem.so must be findable by the linker at build time and at runtime (LD_LIBRARY_PATH or installed to a standard lib path).


Quick start

Add to your module's Cargo.toml:

[dependencies]
xy = "0.1"

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

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

A minimal module (src/lib.rs):

use core::ffi::c_int;
use xy::prelude::*;

// Emit `static mut XY: XyCtx` and `get_xy_ptr` export.
xy_module!();

// Implement the `on_tick` hook: receives dt, returns dt + 1.
#[xy_impl]
pub fn on_tick(dt: c_int) -> c_int {
    dt + 1
}

// Module entry point โ€” called once on first load.
xy_install! {}

Build it as a shared library:

cargo build --release
# produces target/release/libmy_module.so

Load it from a C host (or another Rust binary using xy::load):

xy_load("target/release/libmy_module");
int result = on_tick(10); // 11

Calling another hook from within a module

Use #[xy_decl] to declare an outbound call. The macro generates a per-TU static adapter (no .init_array registration) and an inline dispatch function that routes through the injected XY context.

use core::ffi::c_int;
use xy::prelude::*;

xy_module!();

// Declare on_tick as an outbound call โ€” this module does NOT implement it.
#[xy_decl]
pub fn on_tick(dt: c_int) -> c_int {}

// rs_relay calls on_tick internally and doubles the result.
#[xy_impl]
pub fn rs_relay(val: c_int) -> c_int {
    unsafe { on_tick(val) * 2 }
}

xy_install! {}

Defining a hook for other modules to implement

Use #[xy_def] to register the canonical adapter for a hook. Other modules implement it with #[xy_impl]. Dispatch routes through XY.call โ€” any module can define hooks.

use core::ffi::c_int;
use xy::prelude::*;

xy_module!();

// Define the hook โ€” this module owns the canonical adapter.
// Other modules implement it via #[xy_impl].
#[xy_def]
pub fn my_event(val: c_int) -> c_int {}

// Call it from a listener in this same module.
#[xy_impl]
pub fn trigger(val: c_int) -> c_int {
    unsafe { my_event(val) }
}

xy_install! {}

Per-region state

Modules can declare per-region state. The framework allocates one instance per region the module is loaded into and injects it via XY.region_state before each hook dispatch.

use core::ffi::c_int;
use xy::prelude::*;

xy_module!();

xy_region_state! {
    pub counter: c_int,
}
xy_region_init!();

// Optional: called by the host on unload to free resources.
#[no_mangle]
pub extern "C" fn xy_region_cleanup(_state: *mut core::ffi::c_void) {}

#[xy_impl]
pub fn increment(dummy: c_int) -> c_int {
    let _ = dummy;
    unsafe {
        let s = XY_RS!(XyRegionState);
        (*s).counter += 1;
        (*s).counter
    }
}

xy_install! {}

Claiming a child region

xy_module!();
xy_claim!(2); // request a 2-bit child region

xy_install! {
    // xy_claim is evaluated by the parent's claim handler.
    // If approved, subsequent xy_load / xy_deny calls operate on
    // the child region.
}

Safe wrappers

Inside a module, the following functions are available via xy::prelude::*. They all operate through the injected XY context (set by the host before each hook dispatch) and return Result<(), XyError> or the relevant value.

FunctionSignatureDescription
load(xy, path)unsafe fn load(xy: &XyCtx, path: &CStr) -> Result<(), XyError>Load a module by path (no .so extension)
unload(xy, path)unsafe fn unload(xy: &XyCtx, path: &CStr) -> Result<(), XyError>Unload a previously loaded module
reload(xy, path)unsafe fn reload(xy: &XyCtx, path: &CStr) -> Result<(), XyError>Reload a module in-place
deny(xy, what, ty)unsafe fn deny(xy: &XyCtx, what: &CStr, ty: XyDenyType) -> Result<(), XyError>Deny a hook or module in the current region
require_claim(xy, handler, ud)unsafe fn require_claim(xy: &XyCtx, handler: Option<XyClaimHandlerFn>, ud: *mut c_void) -> Result<(), XyError>Set a claim handler for child regions
region_each(xy, f, ud)unsafe fn region_each(xy: &XyCtx, f: Option<XyRegionEachCbFn>, ud: *mut c_void) -> Result<(), XyError>Iterate over immediate child regions. XyRegionEachCbFn is fn(child_id: u64, plen: u8, ud: *mut c_void) โ€” the width is reported alongside the id
with_region(xy, region_id, plen, f, ud)unsafe fn with_region(xy: &XyCtx, region_id: u64, plen: u8, f: Option<XyScopeFn>, ud: *mut c_void) -> Result<(), XyError>Execute a closure in the context of region (region_id, plen). NotFound unless that pair exists
current_region(xy)unsafe fn current_region(xy: &XyCtx) -> u64Return the current region's id half only โ€” not a unique name
current_region_plen(xy)unsafe fn current_region_plen(xy: &XyCtx) -> u8Return the current region's width in bits (the other half of the identity). 0 is the root, not a sentinel
region_exists(xy, region_id, plen)unsafe fn region_exists(xy: &XyCtx, region_id: u64, plen: u8) -> Result<(), XyError>Whether region (region_id, plen) exists. Both halves required: four regions share id == 0
A region is identified by the pair (id, plen), never the id alone. The
leftmost child of a region has exactly its parent's numeric id, so
(0, 0), (0, 16), (0, 17) and (0, 64) are four distinct regions that all
report region_id == 0. with_region and region_exists therefore both
require plen, and current_region must be paired with
current_region_plen. The id-only xy_region_plen(id) lookup was removed:
looking up by id alone is a query rather than a lookup, and its uint8_t return
could not tell the root's real width of 0 from "not found".

Example โ€” a module that loads a peer module and then operates in a child region (the child must be named by id and width):

use core::ffi::{c_int, c_void};
use xy::prelude::*;

xy_module!();

#[xy_impl]
pub fn setup(_dummy: c_int) -> c_int {
    unsafe {
        // Load another module.  XY is injected by xy_module!().
        load(&XY, c"path/to/peer_module").expect("peer load failed");
    }
    0
}

xy_install! {}

Macro reference

Macro / attributeWhereWhat it does
xy_module!()crate rootEmits static mut XY: XyCtx and get_xy_ptr export
#[xy_impl]module fnRegisters fn as a hook listener (adapter + .init_array entry)
#[xy_decl]module fnDeclares an outbound hook call (per-TU adapter, no registration)
#[xy_def]module fnDefines canonical adapter + dispatch fn; routes through XY.call
xy_install! { }crate rootWraps body as extern "C" fn xy_install()
xy_claim!(bits)crate rootExports xy_claim: u8 symbol
xy_region_state! { }crate rootDeclares XyRegionState struct
xy_region_init!()after stateExports xy_region_state_size()
XY_RS!(Type)hook bodyTyped pointer to current region state

Running the test suite

The tests require libxylem built locally:

cd /path/to/libxylem
make test

This builds both the C test harness and all Rust modules, then runs every test binary including test_rust.


License

BSD-2-Clause โ€” see LICENSE.