Crates.io | ocaml-interop |
lib.rs | ocaml-interop |
version | 0.10.0 |
source | src |
created_at | 2020-09-30 16:03:32.132648 |
updated_at | 2024-01-28 14:20:47.130704 |
description | Utilities for Rust and OCaml interoperability |
homepage | https://github.com/tizoc/ocaml-interop |
repository | https://github.com/tizoc/ocaml-interop |
max_upload_size | |
id | 294654 |
size | 156,596 |
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
ocaml-interop is an OCaml<->Rust FFI with an emphasis on safety inspired by caml-oxide, ocaml-rs and CAMLroot.
Read the full documentation here.
Report issues on Github.
let rust_string = ocaml_string.to_rust();
// `cr` = OCaml runtime handle
let new_ocaml_string = rust_string.to_ocaml(cr);
(* OCaml *)
type my_record = {
string_field: string;
tuple_field: (string * int);
}
// Rust
struct MyStruct {
string_field: String,
tuple_field: (String, i64),
}
impl_conv_ocaml_record! {
MyStruct {
string_field: String,
tuple_field: (String, i64),
}
}
// ...
let rust_struct = ocaml_record.to_rust();
let new_ocaml_record = rust_struct.to_ocaml(cr);
(* OCaml *)
type my_variant =
| EmptyTag
| TagWithInt of int
// Rust
enum MyEnum {
EmptyTag,
TagWithInt(i64),
}
impl_conv_ocaml_variant! {
MyEnum {
EmptyTag,
TagWithInt(OCamlInt),
}
}
// ...
let rust_enum = ocaml_variant.to_rust();
let new_ocaml_variant = rust_enum.to_ocaml(cr);
(* OCaml *)
Callback.register "ocaml_print_endline" print_endline
// Rust
ocaml! {
fn ocaml_print_endline(s: String);
}
// ...
let ocaml_string = "hello OCaml!".to_boxroot(cr);
ocaml_print_endline(cr, &ocaml_string);
// Rust
ocaml_export! {
pub fn twice_boxed_int(cr, num: OCamlRef<OCamlInt64>) -> OCaml<OCamlInt64> {
let num = num.to_rust(cr);
let result = num * 2;
result.to_ocaml(cr)
}
}
(* OCaml *)
external rust_twice_boxed_int: int64 -> int64 = "twice_boxed_int"
(* ... *)
let result = rust_twice_boxed_int 123L in
(* ... *)