[][src]Macro ocaml_interop::impl_to_ocaml_variant

macro_rules! impl_to_ocaml_variant {
    ($rust_typ:ty => $ocaml_typ:ty {
        $($t:tt)*
    }) => { ... };
    ($both_typ:ty {
        $($t:tt)*
    }) => { ... };
}

Implements ToOCaml for mapping a Rust enum into an OCaml variant.

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

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: What is important is the order of the tags, not their names.

impl_to_ocaml_variant! {
    // Optionally, if Rust and OCaml types don't match:
    // RustType => OCamlType { ... }
    Movement {
        Movement::StepLeft,
        Movement::StepRight,
        // Tag field names are mandatory
        Movement::Rotate(rotation: OCamlFloat),
    }
}