/// Helper function for serialization. Interacts with the OCaml runtime, so must
/// only be invoked by the OCaml runtime when serializing a custom block.
Helper function for serialization. Interacts with the OCaml runtime, so must
only be invoked by the OCaml runtime when serializing a custom block.
Helper function for serialization . Interacts with the OCaml runtime so must only be invoked by the OCaml runtime when serializing a custom block .
extern "C" fn serialize_value<T: CamlSerialize>(
    value: usize,
    bsize_32: *mut usize,
    bsize_64: *mut usize,
) {
    let _: usize = catch_unwind(|| {
        // Safety: Only called by the OCaml runtime (we don't expose a means of
        // invoking this function from Rust), which provides some OCaml
        // CUSTOM_TAG block as the value.
        let value = unsafe { Value::from_bits(value) };
        // Only called by the OCaml runtime, when serializing
        // a Custom-object managed by the OCaml GC.
        let rc = rc_from_value::<T>(value).unwrap();
        let bytes: Vec<u8> = rc.serialize();
        let bytes_ptr = bytes.as_ptr();
        // Safety: As above, we don't expose a means of invoking this function
        // from Rust--it can only be invoked by the OCaml runtime while
        // serializing a value. It is safe to invoke OCaml serialization
        // functions in this context.
        unsafe {
            let len = bytes.len();
            caml_serialize_int_8(len.try_into().unwrap());
            caml_serialize_block_1(bytes_ptr, len);
            // The size taken up in the data-part of the custom block.
            *bsize_32 = std::mem::size_of::<u32>();
            *bsize_64 = std::mem::size_of::<u64>();
        }
        0
    });
}
extern " " fn serialize_value < T : CamlSerialize > ( value : usize , bsize_32 : * mut usize , bsize_64 : * mut usize , ) { let _ : usize = catch_unwind ( | | { // Safety: Only called by the OCaml runtime (we don't expose a means of // invoking this function from Rust), which provides some OCaml // CUSTOM_TAG block as the value. let value = unsafe { Value :: from_bits ( value ) } ; // Only called by the OCaml runtime, when serializing // a Custom-object managed by the OCaml GC. let rc = rc_from_value :: < T > ( value ) . unwrap ( ) ; let bytes : Vec < u8 > = rc . serialize ( ) ; let bytes_ptr = bytes . as_ptr ( ) ; // Safety: As above, we don't expose a means of invoking this function // from Rust--it can only be invoked by the OCaml runtime while // serializing a value. It is safe to invoke OCaml serialization // functions in this context. unsafe { let len = bytes . len ( ) ; caml_serialize_int_8 ( len . try_into ( ) . unwrap ( ) ) ; caml_serialize_block_1 ( bytes_ptr , len ) ; // The size taken up in the data-part of the custom block. * bsize_32 = std :: mem :: size_of :: < u32 > ( ) ; * bsize_64 = std :: mem :: size_of :: < u64 > ( ) ; } 0 } ) ; }
Helper function for serialization.
Helper function for serialization .
value
usize
bsize_32
*mut usize
bsize_64
*mut usize