[][src]Macro ocaml_interop::impl_from_ocaml_polymorphic_variant

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

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

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),
    }
}