tizoc / ocaml-interop

OCaml<->Rust FFI with an emphasis on safety.
MIT License
135 stars 22 forks source link

Switch to using Boxroot for root handling #27

Closed tizoc closed 3 years ago

tizoc commented 3 years ago

Switching to ocaml-boxroot for roots management.

TODO

tizoc commented 3 years ago

@g2p here is the WIP work on boxroots integration, I don't think the code will change much more, but I still have to finish the docs.

tizoc commented 3 years ago
pub fn make_some(cr: &mut OCamlRuntime, value: String) -> Option<String> {
    ocaml_frame!(cr, (root), {
        let str = to_ocaml!(cr, value, root); // `OCamlRef<String>`
        let result = ocaml::make_some(cr, str);
        result.to_rust()
    })
}

should now be written as:

pub fn make_some(cr: &mut OCamlRuntime, value: String) -> Option<String> {
    let str = value.to_ocaml_rooted(cr); // BoxRoot<String>
    let result = ocaml::make_some(cr, &str); // dereferenced into `OCamlRef<String>`
    result.to_rust(cr)
}

To note:

This BoxRoot<T> is an owned root, it can be passed around (not bound to the stack lifetime like local roots). Once dropped will delete the root on the OCaml side so that the pointed-to value can be collected. An OCaml<'gc, T> value can be recovered with cr.get(boxroot), and boxroots also deref into OCamlRef<T> references that are what OCaml functions expect as inputs.

tizoc commented 3 years ago

cc @zshipko

zshipko commented 3 years ago

Cool! I will create a new ocaml-rs branch later today to start integrating these changes

tizoc commented 3 years ago

Still unsure about the name to_ocaml_rooted, too long and ugly, considering to_boxroot.