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.jlA 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)RustCall.ARTIFACT_EPOCH — Constant
ARTIFACT_EPOCHA 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.
RustCall.SESSION_TOKEN — Constant
SESSION_TOKENA 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.
RustCall.RustCallState — Type
RustCallStateThe 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.
RustCall.SessionToken — Type
SessionTokenThe identity of one Julia process, for a cached answer that must not outlive it.
RustCall.artifact_epoch — Method
artifact_epoch() -> IntThe 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.
RustCall.session_token — Method
session_token() -> SessionTokenThis process's SESSION_TOKEN.
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
- Artifact identity and caching —
ArtifactIdandartifact_key, the one place an artifact is named, and the Scratch.jl cache that stores what it names (artifact_id.jl,cache.jl). - The FFI type contract — the Rust ↔ Julia type table every code path consults, and the type translation shims over it (
ffi_contract.jl,typetranslation.jl). - Front doors —
rust"""..."""and@rust(ruststr.jl,rustmacro.jl). - Compiler and codegen — the
rustcinvocation, the reader for Cargo's JSON and the generatedccalls (compiler.jl,json.jl,codegen.jl). - The FFI manifest — the
rustcall-extractinterface: manifests, inline expansion, specialization and the toolchain fingerprint (manifest.jl). - Cargo projects and dependencies —
// cargo-deps:, dependency resolution, and the generated Cargo projects and their build cache (dependencies.jl,dependency_resolution.jl,cargoproject.jl,cargobuild.jl). - External crates —
@rust_cratebindings (crate_bindings.jl). - Hot reload API — reloading a crate's library while it is in use (
hot_reload.jl); the Hot Reload guide has a runnable example. - PyO3 crates — the wrapper crate built around a PyO3 extension module, its link plan and skip reasons (
pyo3.jl). - Types, memory and ownership —
RustResult,RustOption, the ownership wrappers, the Rust helpers library and the Julia types generated for#[julia]structs (types.jl,memory.jl,structs.jl). - Generics and
#[julia]functions — the generic function registry, monomorphization, and the wrappers for#[julia]free functions (generics.jl,julia_functions.jl). Generics by hand:RustCall.register_generic_function,RustCall.monomorphize_function,RustCall.call_generic_function,RustCall.precompile_generics,RustCall.release_generics; the Generics guide has runnable examples of both automatic and manual monomorphization. - Errors — the exception types (
exceptions.jl). - Load policy — the one load/unload path (
loadpolicy.jl).
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.