pub trait Custom {
const NAME: &'static str;
const OPS: CustomOps;
const FIXED_LENGTH: Option<custom_fixed_length> = None;
const USED: usize = 0usize;
const MAX: usize = 1usize;
fn ops() -> &'static CustomOps { ... }
}
Expand description
Custom
is used to define OCaml types that wrap existing Rust types, but are owned by the
garbage collector
A custom type can only be converted to a Value
using IntoValue
, but can’t be converted from a
value. Once the Rust value is owned by OCaml it should be accessed using ocaml::Pointer
to
avoid reallocating the same value
struct Example(ocaml::Int);
ocaml::custom! (Example);
#[cfg(feature = "derive")]
#[ocaml::func]
pub unsafe fn example() -> Example {
Example(123)
}
#[cfg(feature = "derive")]
#[ocaml::func]
pub unsafe fn example_value(x: ocaml::Pointer<Example>) -> ocaml::Int {
x.as_ref().0
}
Required Associated Constants
Provided Associated Constants
const FIXED_LENGTH: Option<custom_fixed_length> = None
const FIXED_LENGTH: Option<custom_fixed_length> = None
Custom type fixed length
used
parameter to alloc_custom
. This helps determine the frequency of garbage
collection related to this custom type.