Expand description
Zinc-iron alloy coating is used in parts that need very good corrosion protection.
API IS CONSIDERED UNSTABLE AT THE MOMENT AND IS LIKELY TO CHANGE IN THE FUTURE
IMPORTANT: Starting with version 0.11.0
only OCaml 5.x is supported
ocaml-interop is an OCaml<->Rust FFI with an emphasis on safety inspired by caml-oxide, ocaml-rs and CAMLroot.
§Table of Contents
§Usage
This section provides a high-level overview of ocaml-interop
. For detailed explanations,
tutorials, and best practices, please refer to the User Guides module.
§Runtime Initialization and Management
Proper initialization and management of the OCaml runtime is crucial, especially when Rust
code drives the execution. This involves using OCamlRuntime::init
and managing its
lifecycle with OCamlRuntimeStartupGuard
.
For detailed information, see OCaml Runtime (Part 5).
§Acquiring and Using the OCaml Runtime Handle
Most interop operations require an OCaml runtime handle (cr: &mut OCamlRuntime
).
This handle is obtained differently depending on whether Rust calls OCaml or OCaml calls Rust.
See these guides for more details:
§OCaml value representation
OCaml values are represented in Rust using types like OCaml<'gc, T>
,
BoxRoot<T>
, and OCamlRef<'a, T>
, each with specific roles
in memory management and GC interaction.
Learn more in Part 2: Fundamental Concepts.
§Converting between OCaml and Rust data
The traits FromOCaml
and ToOCaml
facilitate data conversion
between Rust and OCaml types.
For conversion details and examples, refer to Part 2: Fundamental Concepts, as well as the guides on exporting and invoking functions.
§Calling convention
ocaml-interop
uses a caller-rooted argument convention for safety, where the caller is
responsible for ensuring arguments are rooted before a function call.
This is explained further in Part 2: Fundamental Concepts.
§OCaml exceptions
By default, Rust panics in exported functions are caught and translated to OCaml exceptions. Conversely, OCaml exceptions raised during calls from Rust will result in Rust panics.
For error handling strategies, see Part 2: Fundamental Concepts and Part 6: Advanced Topics.
§Calling into OCaml from Rust
To call OCaml functions from Rust, they typically need to be registered in OCaml
(e.g., using Callback.register
) and then declared in Rust using the ocaml!
macro.
This setup allows Rust to find and invoke these OCaml functions.
For a comprehensive guide on calling OCaml functions from Rust, including detailed examples and best practices, please see: Invoking OCaml Functions (Part 4).
§Calling into Rust from OCaml
Rust functions can be exposed to OCaml using the #[ocaml_interop::export]
procedural macro, which handles FFI boilerplate, type marshalling, and panic safety.
Attributes like no_panic_catch
, bytecode
, and noalloc
allow customization.
For a detailed guide, see Exporting Rust Functions (Part 3).
§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.
Modules§
- bigarray
- user_
guides - ocaml-interop: OCaml and Rust Integration
Macros§
- impl_
conv_ ocaml_ record - Implements conversion between a Rust struct and an OCaml record.
- impl_
conv_ ocaml_ variant - Implements conversion between a Rust enum and an OCaml variant.
- impl_
from_ ocaml_ polymorphic_ variant - Implements
FromOCaml
for mapping an OCaml polymorphic variant into a Rust enum. - impl_
from_ ocaml_ record - Implements
FromOCaml
for mapping an OCaml record into a Rust record. - impl_
from_ ocaml_ variant - Implements
FromOCaml
for mapping an OCaml variant into a Rust enum. - impl_
to_ ocaml_ polymorphic_ variant - Implements
ToOCaml
for mapping a Rust enum into an OCaml polymorphic variant. - impl_
to_ ocaml_ record - Implements
ToOCaml
for mapping a Rust record into an OCaml record. - impl_
to_ ocaml_ variant - Implements
ToOCaml
for mapping a Rust enum into an OCaml variant. - ocaml
- Declares OCaml functions.
- ocaml_
alloc_ polymorphic_ variant - Allocates an OCaml polymorphic variant, mapped from a Rust enum.
- ocaml_
alloc_ record - Allocates an OCaml record built from a Rust record
- ocaml_
alloc_ tagged_ block - Allocates an OCaml memory block tagged with the specified value.
- ocaml_
alloc_ variant - Allocates an OCaml variant, mapped from a Rust enum.
- ocaml_
unpack_ polymorphic_ variant - Unpacks an OCaml polymorphic variant and maps it into a Rust enum.
- ocaml_
unpack_ record - Unpacks an OCaml record into a Rust record.
- ocaml_
unpack_ variant - Unpacks an OCaml variant and maps it into a Rust enum.
Structs§
- BoxRoot
BoxRoot<T>
is a container for a rootedOCaml
<T>
value.- DynBox
OCaml<DynBox<T>>
is for passing a value of typeT
to OCaml- OCaml
- Representation of OCaml values.
- OCaml
Bytes OCaml
<OCamlBytes>
is a reference to an OCamlbytes
value.- OCaml
Exception OCaml
<OCamlException>
is a reference to an OCamlexn
value.- OCaml
Float OCaml
<OCamlFloat>
is a reference to an OCamlfloat
(boxedfloat
) value.- OCaml
Float Array OCaml
<OCamlFloatArray<T>>
is a reference to an OCamlfloatarray
which is an array containingfloat
s in an unboxed form.- OCaml
Int32 OCaml
<OCamlInt32>
is a reference to an OCamlInt32.t
(boxedint32
) value.- OCaml
Int64 OCaml
<OCamlInt64>
is a reference to an OCamlInt64.t
(boxedint64
) value.- OCaml
List OCaml
<OCamlList<T>>
is a reference to an OCamllist
containing values of typeT
.- OCaml
Runtime - Per-thread handle to the OCaml runtime.
- OCaml
Runtime Startup Guard - RAII guard for the OCaml runtime.
- OCaml
Uniform Array OCaml
<OCamlUniformArray<T>>
is a reference to an OCaml array which is guaranteed to not contain unboxed floats. If OCaml was configured with--disable-flat-float-array
this corresponds to regulararray
s, but if not,Uniform_array.t
in thebase
library can be used instead. See Lexifi’s blog post on the topic for more details.
Traits§
- FromO
Caml - Implements conversion from OCaml values into Rust values.
- ToOCaml
- Implements conversion from Rust values into OCaml values.
Functions§
- alloc_
error - alloc_
ok - cons
- List constructor
Type Aliases§
- OCaml
Fn1 - OCaml function that accepts one argument.
- OCaml
Fn2 - OCaml function that accepts two arguments.
- OCaml
Fn3 - OCaml function that accepts three arguments.
- OCaml
Fn4 - OCaml function that accepts four arguments.
- OCaml
Fn5 - OCaml function that accepts five arguments.
- OCaml
Int OCaml
<OCamlInt>
is an OCaml integer (tagged and unboxed) value.- OCaml
Ref - An
OCamlRef<T>
is a reference to a location containing aOCaml
<T>
value. - RawO
Caml - OCaml
value
type
Attribute Macros§
- export
- Exports a Rust function to OCaml.