pub trait Custom: Sized {
    const NAME: &'static str;
    const OPS: CustomOps;
    const FIXED_LENGTH: Option<custom_fixed_length> = None;
    const USED: usize = 0usize;
    const MAX: usize = 1usize;

    unsafe extern "C" fn finalize(v: Raw) { ... }
    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 ToValue, 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. By default the inner Rust value will be dropped when the finalizer runs on the OCaml side.

struct Example(ocaml::Int);

ocaml::custom! (Example);

#[cfg(feature = "derive")]
#[ocaml::func]
pub unsafe fn example() -> ocaml::Pointer<Example> {
    Example(123).into()
}

#[cfg(feature = "derive")]
#[ocaml::func]
pub unsafe fn example_value(x: &Example) -> ocaml::Int {
    x.0
}

Required Associated Constants

Custom type name

Custom operations

Provided Associated Constants

Custom type fixed length

used parameter to alloc_custom. This helps determine the frequency of garbage collection related to this custom type.

max parameter to alloc_custom. This helps determine the frequency of garbage collection related to this custom type

Provided Methods

Automatically calls Pointer::drop_in_place

Get a static reference the this type’s CustomOps implementation

Implementors