API Reference

The reference is split into one page per group of source files (#288); every docstring in the package is rendered on exactly one of them. This page is the index. Only the macros are exported; everything else is reached as RustCall.name.

RustCall.RustCall — Module
RustCall.jl

A Foreign Function Interface (FFI) package for calling Rust code from Julia through a C-compatible ABI.

Exported Macros

  • @rust: Call a registered Rust function
  • @rust_str: Compile and register Rust code (rust"" string literal)

Example

using RustCall

# Define Rust code
rust"""
#[no_mangle]
pub extern "C" fn add(a: i32, b: i32) -> i32 {
    a + b
}
"""

# Call the function
result = @rust add(10i32, 20i32)
source
RustCall.ARTIFACT_EPOCH — Constant
ARTIFACT_EPOCH

A counter bumped by every write to the state container, so a caller that resolved something out of it can tell, without taking a lock, whether its answer is still the current one (#253).

Why a counter and not a flag

A call site may keep the CallTarget it resolved (cached_call_target, src/macros/ruststr.jl) and reuse it instead of paying resolve_call_target again — 7 µs and a hundred allocations, on the way to a 5 ns ccall. That is only sound while nothing the snapshot captured has changed: a hot reload, an adoption, an alias, a retirement, a newly registered return type or panic channel all make a kept snapshot a pointer into the wrong generation, which is the #277 bug class.

So the reuse has to be invalidated, and nothing may be allowed to forget to invalidate it. The bump therefore lives in _state_mutate_storage! (src/loading/state_filter.jl) — the one helper every state-container write already goes through — rather than at the mutation sites, which are many and which grow. A write that does not actually change what a snapshot would say costs a re-resolution and nothing else; a write that does and went unnoticed would be a call into an unmapped image. The counter is deliberately conservative in the only direction that is safe.

It is read with a plain atomic load and never taken under REGISTRY_LOCK, which is what takes the lock off the calling path entirely.

source
RustCall.SESSION_TOKEN — Constant
SESSION_TOKEN

A freshly allocated SessionToken, replaced by __init__ in every process, and the first half of what makes a cached CallTarget valid.

Why an object and not a number

A CallTargetCache is spliced into the body of the wrapper it belongs to, so a package that calls a generated wrapper from a precompile workload serialises that cache into its .ji file with a populated entry — and the entry holds raw pointers belonging to the process that wrote them. ARTIFACT_EPOCH cannot tell: it starts at the same value in every process, so a deserialised epoch can equal a live one, and the entry would be accepted and its pointer called. That is a ccall into a process that no longer exists (#390 review).

Identity settles it with certainty rather than probability. A deserialised entry carries the token object of the process that wrote it; this process allocated its own in __init__, and two distinct objects are never ===. No counter collision, and no random seed that is merely unlikely to repeat, can make a foreign entry validate.

source
RustCall.RustCallState — Type
RustCallState

The mutable process state of RustCall. The value is held by STATE, a Base.Lockable, so registry objects are never declared as independent module globals. StateView is a compatibility handle used by the older internal names; every operation on it takes the state lock, including reads made by tests and extensions.

Static lookup tables are immutable dictionaries or tuples, not mutable registries. test/test_state.jl inspects all module bindings, including values returned by factories and containers nested in immutable wrappers, so a newly named mutable registry cannot bypass the declaration guard. The compiler's documentation metadata and synchronization primitives are not application registries; captured per-object liveness flags belong to the state-owned images.

Code under the state lock may only manipulate in-memory state. It must not call user Julia code, compile, resolve symbols, open/close libraries, or execute a ccall. Those operations happen before or after the locked transaction. Fetch the FFI method-definition gate from STATE before acquiring it; never acquire that gate while holding STATE, and run layout callbacks outside both.

source
RustCall.artifact_epoch — Method
artifact_epoch() -> Int

The current value of ARTIFACT_EPOCH. Read this before resolving anything that will be cached against it: a mutation landing between the read and the resolution then invalidates the cached answer, where reading it afterwards could stamp a stale snapshot with a current epoch.

source

Macros

  • @rust_str — rust"""...""": compile a Rust snippet and register its functions.
  • @rust — call a registered Rust function.
  • @rust_crate — bind an external crate.

@rust_llvm and the LLVM IR integration path were removed in 0.3.0 (#265); use @rust. @irust and irust"..." were removed in 0.9.0 (#538); write the snippet as a #[julia] function in rust"""...""" (see Migrating from @irust).

Pages

Internal registries and constants (RustCall.RUST_LIBRARIES, RustCall.CURRENT_LIB, RustCall.GENERIC_FUNCTION_REGISTRY, …) are rendered on the page of the file that defines them. They are implementation details, documented for completeness, and should not be accessed directly by users.