Trait Custom

Source
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;

    // Provided methods
    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§

Source

const NAME: &'static str

Custom type name

Source

const OPS: CustomOps

Custom operations

Provided Associated Constants§

Source

const FIXED_LENGTH: Option<custom_fixed_length> = None

Custom type fixed length

Source

const USED: usize = 0usize

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

Source

const MAX: usize = 1usize

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

Provided Methods§

Source

unsafe extern "C" fn finalize(v: Raw)

Automatically calls Pointer::drop_in_place

Source

fn ops() -> &'static CustomOps

Get a static reference the this type’s CustomOps implementation

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§