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§
Provided Associated Constants§
Sourceconst FIXED_LENGTH: Option<custom_fixed_length> = None
const FIXED_LENGTH: Option<custom_fixed_length> = None
Custom type fixed length
Provided Methods§
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.