Macro ocaml_interop::ocaml_alloc_polymorphic_variant[][src]

macro_rules! ocaml_alloc_polymorphic_variant {
    ($cr : ident, $self : ident =>
 {
     $($($tag : ident) :: +
       $(($($slot_name : ident : $slot_typ : ty), + $(,) ?)) ? $(,) ?), +
 }) => { ... };
}
Expand description

Allocates an OCaml polymorphic variant, mapped from a Rust enum.

The match in this conversion is exhaustive, and requires that every enum case is covered.

Although the order of the tags doesn’t matter, the Rust and OCaml names must match exactly. For tags containing multiple values, it is important that the order of the fields remains the same as in the OCaml type declaration.

Examples

enum Movement {
    StepLeft,
    StepRight,
    Rotate(f64),
}

// Assuming an OCaml type declaration like:
//
//      type movement = [
//        | `StepLeft
//        | `StepRight
//        | `Rotate of float
//      ]
//
// NOTE: Order of tags is irrelevant but names must match exactly.

let movement = Movement::Rotate(180.0);
let ocaml_movement: OCaml<Movement> = ocaml_alloc_polymorphic_variant! {
    cr, movement => {
        Movement::StepLeft,
        Movement::StepRight,
        // Tag field names are mandatory
        Movement::Rotate(rotation: OCamlFloat),
    }
};
// ...