[−][src]Macro ocaml_interop::ocaml_unpack_variant
Unpacks an OCaml variant and maps it into a Rust enum.
It is important that the order remains the same as in the OCaml type declaration.
Note
Unlike with ocaml_unpack_record!
, the result of ocaml_unpack_variant!
is a Result
value.
An error will be returned in the case of an expected tag value. This may change in the future.
Examples
enum Movement { StepLeft, StepRight, Rotate(f64), } // Assuming an OCaml type declaration like: // // type my_struct = // | StepLeft // | StepRight // | Rotate of float // // NOTE: What is important is the order of the tags, not their names. let ocaml_variant = ocaml_call!(make_ocaml_movement(gc, OCaml::unit())).unwrap(); let result = ocaml_unpack_variant! { ocaml_variant => { // Alternative: StepLeft => Movement::StepLeft // <anyname> => <build-expr> Movement::StepLeft, Movement::StepRight, // Tag field names are mandatory Movement::Rotate(rotation: OCamlFloat), } }.unwrap(); // ...