$ show Taaitaaiger/jlrs
jlrs is a Rust library that provides safe, high-performance bindings to the Julia C API, enabling both embedding Julia within Rust applications and exporting Rust functions as Julia modules (via ccall). The architecture is modular, with separate crates for raw FFI (`jl_sys`, `jlrs_sys`), a proc-macro crate (`jlrs_macros`) for derive macros and the `julia_module!` macro, and the main `jlrs` crate offering managed types (`Value`, `Array`, `DataType`), memory safety through scoped GC frames, and multiple runtime modes (local, async via tokio, multi-threaded). The library handles Juliaβs garbage collector carefully with custom GC-safe locks and fast thread-local state, and supports advanced features like static arrays, ndarray integration, and opaque Rust types managed by Julia. Key entry points are the `Builder` struct for initializing Julia, runtime handles (`LocalHandle`, `AsyncHandle`, `MtHandle`), and the `julia_module!` macro for exposing Rust code. The codebase is well-tested with integration tests, benchmarks, and CI across Julia LTS and stable versions on Linux, macOS, and Windows.
The LLM identified 3 files worth highlighting for reuse:
Implements `GcSafeRwLock`, a read-write lock that prevents deadlocks with Juliaβs garbage collector by ensuring no GC-unsafe work is done while holding the lock. The pattern involves storing the GC state at acquisition and restoring it on release. This is a self-contained, extractable synchronization primitive useful for any Rust library that must safely interact with a garbage-collected runtime from multiple threads.
Provides `StaticRef`, `StaticGlobal`, and `StaticSymbolRef` with associated macros (`define_static_global!`, `inline_static_global!`) for lazily initializing and caching Julia global objects, symbols, and type references. Uses `AtomicPtr` and `GcSafeOnceLock` for thread-safe one-time initialization. This pattern is reusable for any FFI crate that needs efficient, safe, and lazy access to foreign runtime globals without repeated lookup overhead.
Implements the `julia_module!` proc-macro, the primary entry point for exporting Rust items (functions, types, constants) as a Julia module. The macro parses a custom DSL (AST β Model β IR β Codegen), generating C-callable wrappers, type registration, and Julia-side definitions. This is a non-trivial metaprogramming pipeline that demonstrates advanced macro techniques (token stream parsing, attribute handling, multi-stage lowering) and could serve as a template for other language binding generators.