ocaml-interop
Zinc-iron alloy coating is used in parts that need very good corrosion protection.
ocaml-interop is an OCaml<->Rust FFI with an emphasis on safety inspired by caml-oxide and ocaml-rs.
Read the full documentation here.
Report issues on Github.
Table of Contents
How does it work
ocaml-interop, just like caml-oxide, encodes the invariants of OCaml's garbage collector into the rules of Rust's borrow checker. Any violation of these invariants results in a compilation error produced by Rust's borrow checker.
This requires that the user is explicit about delimiting blocks that interact with the OCaml runtime, and that calls into the OCaml runtime are done only inside these blocks, and wrapped by a few special macros.
A quick taste
Convert between plain OCaml and Rust values
let rust_string = ocaml_string.into_rust;
let new_ocaml_string = to_ocaml!;
Convert between Rust and OCaml structs/records
(* OCaml *)
// Rust
impl_conv_ocaml_record!
// ...
let rust_struct = ocaml_record.into_rust;
let new_ocaml_record = to_ocaml!;
Convert between OCaml and Rust variants/enums
(* OCaml *)
// Rust
impl_conv_ocaml_variant!
// ...
let rust_enum = ocaml_variant.into_rust;
let new_ocaml_variant = to_ocaml!;
Call OCaml functions from Rust
(* OCaml *)
register "ocaml_print_endline" print_endline
ocaml!
// ...
let ocaml_string = to_ocaml!;
ocaml_call!.unwrap;
Call Rust functions from OCaml
// Rust
ocaml_export!
(* OCaml *)
(* ... *)
let result = rust_twice_boxed_int 123L in
(* ... *)
References and links
- OCaml Manual: Chapter 20 Interfacing C with OCaml.
- Safely Mixing OCaml and Rust paper by Stephen Dolan.
- Safely Mixing OCaml and Rust talk by Stephen Dolan.
- CAMLroot: revisiting the OCaml FFI.
- caml-oxide, the code from that paper.
- ocaml-rs, another OCaml<->Rust FFI library.