Macro ocaml_interop::impl_to_ocaml_record
source · macro_rules! impl_to_ocaml_record { ($rust_typ:ty => $ocaml_typ:ident { $($field:ident : $ocaml_field_typ:ty $(=> $conv_expr:expr)?),+ $(,)? }) => { ... }; ($both_typ:ident { $($t:tt)* }) => { ... }; }
Expand description
Implements ToOCaml
for mapping a Rust record into an OCaml record.
It is important that the order of the fields remains the same as in the OCaml type declaration.
Examples
struct MyStruct {
int_field: u8,
string_field: String,
}
// Assuming an OCaml record declaration like:
//
// type my_struct = {
// int_field: int;
// string_field: string;
// }
//
// NOTE: What is important is the order of the fields, not their names.
impl_to_ocaml_record! {
// Optionally, if Rust and OCaml types don't match:
// RustType => OCamlType { ... }
MyStruct {
// optionally `=> expr` can be used to preprocess the field value
// before the conversion into OCaml takes place.
// Inside the expression, a variable with the same name as the field
// is bound to a reference to the field value.
int_field: OCamlInt => { *int_field as i64 },
string_field: String,
}
}