Macro ocaml_interop::impl_from_ocaml_polymorphic_variant[][src]

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

Implements FromOCaml for mapping an OCaml polymorphic variant into a Rust enum.

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
//      ]

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