Macro ocaml_interop::ocaml [−][src]
macro_rules! ocaml { () => { ... }; ($vis:vis fn $name:ident( $arg:ident: $typ:ty $(,)? ) $(-> $rtyp:ty)?; $($t:tt)*) => { ... }; ($vis:vis fn $name:ident( $arg1:ident: $typ1:ty, $arg2:ident: $typ2:ty $(,)? ) $(-> $rtyp:ty)?; $($t:tt)*) => { ... }; ($vis:vis fn $name:ident( $arg1:ident: $typ1:ty, $arg2:ident: $typ2:ty, $arg3:ident: $typ3:ty $(,)? ) $(-> $rtyp:ty)?; $($t:tt)*) => { ... }; ($vis:vis fn $name:ident( $($arg:ident: $typ:ty),+ $(,)? ) $(-> $rtyp:ty)?; $($t:tt)*) => { ... }; }
Declares OCaml functions.
ocaml! { pub fn registered_name(arg1: ArgT, ...) -> Ret_typ; ... }
declares a function that has been
defined in OCaml code and registered with Callback.register "registered_name" ocaml_function
.
Visibility and return value type can be omitted. The return type defaults to ()
when omitted.
When invoking one of these functions, the first argument must be a &mut
OCamlRuntime
,
and the remaining arguments OCamlRef
<ArgT>
.
The return value is a BoxRoot
<RetType>
.
Calls that raise an OCaml exception will panic!
. Care must be taken on the OCaml side
to avoid exceptions and return ('a, 'err) Result.t
values to signal errors, which
can then be converted into Rust’s Result<A, Err>
and Result<OCaml<A>, OCaml<Err>>
.
Examples
ocaml! { // Declares `print_endline`, with a single `String` (`OCamlRef<String>` when invoked) // argument and `BoxRoot<()>` return type (default when omitted). pub fn print_endline(s: String); // Declares `bytes_concat`, with two arguments, an OCaml `bytes` separator, // and an OCaml list of segments to concatenate. Return value is an OCaml `bytes` // value. fn bytes_concat(sep: OCamlBytes, segments: OCamlList<OCamlBytes>) -> OCamlBytes; }