Macro ocaml_interop::ocaml_alloc_record[][src]

macro_rules! ocaml_alloc_record {
    ($cr : ident, $self : ident
 { $($field : ident : $ocaml_typ : ty $(=> $conv_expr : expr) ?), + $(,) ? }) => { ... };
}
Expand description

Allocates an OCaml record built from a Rust record

Most of the time the impl_to_ocaml_record! macro will be used to define how records should be converted. This macro is useful when implementing OCaml allocation functions directly.

It is important that the order of the fields remains the same as in the OCaml type declaration.

Examples

struct MyStruct {
    int_field: u8,
    string_field: String,
}

// Assuming an OCaml record declaration like:
//
//      type my_struct = {
//          int_field: int;
//          string_field: string;
//      }
//
// NOTE: What is important is the order of the fields, not their names.

let ms = MyStruct { int_field: 132, string_field: "blah".to_owned() };
let ocaml_ms: OCaml<MyStruct> = ocaml_alloc_record! {
    //  value { field: OCamlType, ... }
    cr, ms {  // cr: &mut OCamlRuntime
        // optionally `=> expr` can be used to pre-process the field value
        // before the conversion into OCaml takes place.
        // Inside the expression, a variable with the same name as the field
        // is bound to a reference to the field value.
        int_field: OCamlInt => { *int_field as i64 },
        string_field: String,
    }
};
// ...