A safe and ergonomic wrapper around Wolfram LibraryLink.
Wolfram LibraryLink is an interface for dynamic libraries that can be dynamically loaded by the Wolfram Language. This crate provides idiomatic Rust bindings around the lower-level LibraryLink C interface.
This library provides functionality for:
- Calling Rust functions from Wolfram code.
- Passing data efficiently between Rust and Wolfram code using native data types
like [
NumericArray] and [Image]. - Passing arbitrary expressions between Rust and Wolfram code using
[
Expr][struct@crate::Expr] and the [export_wstp!][crate::export_wstp] macro. - Asynchronous events handled by the Wolfram Language, using an [
AsyncTaskObject] background thread.
Related Links
Examples
Writing a Rust function that can be called from the Wolfram Language is as easy as writing:
#
building your dynamic library, and loading the function into the Wolfram Language
using LibraryFunctionLoad:
func = LibraryFunctionLoad["library_name", "square", {Integer}, Integer];
func[5] (* Returns 25 *)
Show backtrace when a panic occurs
Functions backed by a WSTP [Link] (using [export_wstp![]][crate::export_wstp]) will
automatically catch any
Rust panics that occur in the wrapped code, and return a Failure object
with the panic message and source file/line number. It also can optionally show the
backtrace. This is configured by the "LIBRARY_LINK_RUST_BACKTRACE" environment
variable. Enable it by evaluating:
SetEnvironment["LIBRARY_LINK_RUST_BACKTRACE" -> "True"]
Now the error shown when a panic occurs will include a backtrace.
Note that the error message may include more information if the "nightly"
feature of wolfram-library-link is enabled.