Skip to contents

uniffi_load() opens a library built from a Rust crate that uses UniFFI 0.33, and evaluates the R bindings that uniffi-bindgen-r wrote for it. It returns the crate's functions: m$f(x) calls the crate's f(), and m$Thing_new() its object Thing's constructor new, whose result has its methods (obj$method()).

Usage

uniffi_load(path, bindings = NULL, int64 = c("double", "zuint"))

Arguments

path

The library: a path, or any name ffi_dlopen() accepts.

bindings

The bindings' file, or NULL for the file beside the library that uniffi-bindgen-r names after the crate.

int64

How 64-bit integers arrive, as in ffi_dlopen(): as doubles when exact, or as zuint's types.

Value

An environment, locked, holding the crate's functions and constructors. The library's handle is in it as .lib; ffi_close() refuses it while one of the crate's objects lives.

Details

Build the crate as a cdylib (cargo build --release), then run uniffi-bindgen-r on the library, which writes the bindings beside it, named after the crate. That is where uniffi_load() looks by default: for target/release/libmy_crate.so, target/release/my_crate.R (my_crate.dll and my_crate.R on Windows). Rust and cargo are needed to build the crate and the bindings, never to load them.

Before anything else is called, the library's UniFFI contract version and every function's checksum are compared with the ones the bindings were generated for. A library and bindings from different builds of the crate refuse to load with a zuffi_contract_error naming the functions that differ, rather than corrupting memory. zuffi reads contract version 33 (UniFFI 0.33) only.

How values cross, which Rust types are supported, and the classed conditions a Rust error becomes, are in the design, §7.2 and §7.3. Briefly: records are named lists, flat enums strings, enums with data lists with a variant field, Option<T> NULL or the value, objects environments whose Rust side is freed by the garbage collector or at once by ffi_free(), and an Err a condition of class c("<crate>_<Error>", "uniffi_error", "zuffi_error").

The bindings are R code, evaluated as source() would: load only files you trust, as you would only load libraries you trust.

Examples

if (FALSE) { # \dontrun{
# cargo build --release && uniffi-bindgen-r target/release/libmy_crate.so
m <- uniffi_load("target/release/libmy_crate.so")
m$add(1L, 2L)
} # }