Macro ocaml_interop::ocaml_export[][src]

macro_rules! ocaml_export {
    { } => { ... };
    {
    fn $name : ident($cr : ident, $($args : tt) *) -> f64 $body : block
    $($t : tt) *
} => { ... };
    {
    fn $name : ident($cr : ident, $($args : tt) *) $(-> $rtyp : ty) ? $body :
    block $($t : tt) *
} => { ... };
    {
    fn $name : ident($($invalid_args : tt) *) $(-> $rtyp : ty) ? $body : block
    $($t : tt) *
} => { ... };
}
Expand description

Defines Rust functions callable from OCaml.

The first argument in these functions declarations is a name to bind a &mut OCamlRuntime.

Arguments and return values must be of type OCamlRef<T>, or f64 in the case of unboxed floats.

The return type defaults to OCaml<()> when omitted.

Examples

ocaml_export! {
    fn rust_twice(cr, num: OCamlRef<OCamlInt>) -> OCaml<OCamlInt> {
        let num: i64 = num.to_rust(cr);
        unsafe { OCaml::of_i64_unchecked(num * 2) }
    }

    fn rust_twice_boxed_i32(cr, num: OCamlRef<OCamlInt32>) -> OCaml<OCamlInt32> {
        let num: i32 = num.to_rust(cr);
        let result = num * 2;
        result.to_ocaml(cr)
    }

    fn rust_add_unboxed_floats_noalloc(_cr, num: f64, num2: f64) -> f64 {
        num * num2
    }

    fn rust_twice_boxed_float(cr, num: OCamlRef<OCamlFloat>) -> OCaml<OCamlFloat> {
        let num: f64 = num.to_rust(cr);
        let result = num * 2.0;
        result.to_ocaml(cr)
    }

    fn rust_increment_ints_list(cr, ints: OCamlRef<OCamlList<OCamlInt>>) -> OCaml<OCamlList<OCamlInt>> {
        let mut vec: Vec<i64> = ints.to_rust(cr);

        for i in 0..vec.len() {
            vec[i] += 1;
        }

        vec.to_ocaml(cr)
    }

    fn rust_make_tuple(cr, fst: OCamlRef<String>, snd: OCamlRef<OCamlInt>) -> OCaml<(String, OCamlInt)> {
        let fst: String = fst.to_rust(cr);
        let snd: i64 = snd.to_rust(cr);
        let tuple = (fst, snd);
        tuple.to_ocaml(cr)
    }
}