Macro ocaml_interop::ocaml_unpack_polymorphic_variant [−][src]
macro_rules! ocaml_unpack_polymorphic_variant {
($self : ident =>
{
$($tag : ident $(($($slot_name : ident : $slot_typ : ty), + $(,) ?)) ? =>
$conv : expr), + $(,) ?
}) => { ... };
}
Expand description
Unpacks an OCaml polymorphic variant and maps it into a Rust enum.
Note
Unlike with ocaml_unpack_record!
, the result of ocaml_unpack_polymorphic_variant!
is a Result
value.
An error will be returned in the case of an unexpected tag value. This may change in the future.
Examples
enum Movement {
StepLeft,
StepRight,
Rotate(f64),
}
// Assuming an OCaml type declaration like:
//
// type movement = [
// | `StepLeft
// | `StepRight
// | `Rotate of float
// ]
let ocaml_polymorphic_variant_root = make_ocaml_polymorphic_movement(cr, &OCaml::unit());
let ocaml_polymorphic_variant = cr.get(&ocaml_polymorphic_variant_root);
let result = ocaml_unpack_polymorphic_variant! {
ocaml_polymorphic_variant => {
StepLeft => Movement::StepLeft,
StepRight => Movement::StepRight,
// Tag field names are mandatory
Rotate(rotation: OCamlFloat)
=> Movement::Rotate(rotation),
}
}.unwrap();
// ...