/// Helper function for deserialization. Interacts with the OCaml runtime, so must
/// only be invoked by the OCaml runtime when serializing a custom block.
Helper function for deserialization. Interacts with the OCaml runtime, so must
only be invoked by the OCaml runtime when serializing a custom block.
Helper function for deserialization . Interacts with the OCaml runtime so must only be invoked by the OCaml runtime when serializing a custom block .
extern "C" fn deserialize_value<T: CamlSerialize>(data_ptr: *mut c_void) -> usize {
    catch_unwind(|| {
        // Get the serialized bytes from the input channel.
        let bytes = unsafe {
            // Safety: We don't expose a means of invoking this function from
            // Rust--`deserialize_value` can only be invoked by the OCaml
            // runtime while deserializing a custom block value. It is safe to
            // invoke OCaml deserialization functions in this context.
            let len: usize = caml_deserialize_sint_8().try_into().unwrap();
            let mut buf: Vec<u8> = Vec::with_capacity(len);
            // Safety: len <= capacity. The elements aren't initialized at this
            // time, but we trust that caml_deserialize_block_1 will fill `len`
            // bytes of the buffer.
            buf.set_len(len);
            // Safety: As above, `deserialize_value` can only be invoked by the
            // OCaml runtime during custom block deserialization.
            caml_deserialize_block_1(buf.as_mut_ptr(), len);
            buf
        };
        // Actually deserialize those bytes into a T.
        let val: T = CamlSerialize::deserialize(&bytes);
        // Safety: The OCaml runtime will give us a data buffer which is
        // usize-aligned and valid for reads and writes of bsize_32 or bsize_64
        // (as provided by `serialize_value`, above) bytes (depending on system
        // architecture). This is sufficient for `Rc<T>` (which has the size and
        // alignment of usize).
        let data_ptr = data_ptr as *mut MaybeUninit<Rc<T>>;
        let data = unsafe { data_ptr.as_mut().unwrap() };
        *data = MaybeUninit::new(Rc::new(val));
        // Return the size of the value we wrote to our output pointer. The
        // OCaml runtime will verify that it matches the expected
        // bsize_32/bsize_64 written by the serializer.
        std::mem::size_of_val(data)
    })
}
extern " " fn deserialize_value < T : CamlSerialize > ( data_ptr : * mut c_void ) -> usize { catch_unwind ( | | { // Get the serialized bytes from the input channel. let bytes = unsafe { // Safety: We don't expose a means of invoking this function from // Rust--`deserialize_value` can only be invoked by the OCaml // runtime while deserializing a custom block value. It is safe to // invoke OCaml deserialization functions in this context. let len : usize = caml_deserialize_sint_8 ( ) . try_into ( ) . unwrap ( ) ; let mut buf : Vec < u8 > = Vec :: with_capacity ( len ) ; // Safety: len <= capacity. The elements aren't initialized at this // time, but we trust that caml_deserialize_block_1 will fill `len` // bytes of the buffer. buf . set_len ( len ) ; // Safety: As above, `deserialize_value` can only be invoked by the // OCaml runtime during custom block deserialization. caml_deserialize_block_1 ( buf . as_mut_ptr ( ) , len ) ; buf } ; // Actually deserialize those bytes into a T. let val : T = CamlSerialize :: deserialize ( & bytes ) ; // Safety: The OCaml runtime will give us a data buffer which is // usize-aligned and valid for reads and writes of bsize_32 or bsize_64 // (as provided by `serialize_value`, above) bytes (depending on system // architecture). This is sufficient for `Rc<T>` (which has the size and // alignment of usize). let data_ptr = data_ptr as * mut MaybeUninit < Rc < T > > ; let data = unsafe { data_ptr . as_mut ( ) . unwrap ( ) } ; * data = MaybeUninit :: new ( Rc :: new ( val ) ) ; // Return the size of the value we wrote to our output pointer. The // OCaml runtime will verify that it matches the expected // bsize_32/bsize_64 written by the serializer. std :: mem :: size_of_val ( data ) } ) }
Helper function for deserialization.
Helper function for deserialization .
data_ptr
*mut c_void