Skip to content

Investigate how to increase compatibility with ocaml-rs to ease integration #13

@tizoc

Description

@tizoc

WIP

Motivation

ocaml-rs already solves a bunch of things that are planned in ocaml-interop, and ocaml-interop already provides a safe API that could benefit ocaml-rs. The idea her is to figure out what would be necessary for "sandwiching" ocaml-interop in-between ocaml-sys and ocaml-rs's higher level APIs.

What ocaml-interop solves for ocaml-rs

  • Lifetimes and proper abstractions to enforce correct usage of OCaml values across OCaml allocations
  • More accurate types for OCaml values. ocaml-rs represents OCaml values as Value, ocaml-interop uses OCaml<T>.
  • More fine-grained definitions for OCaml values, this removes a whole class of errors (so there is no need to validate operations performed on an OCaml value, because you know it is of the correct type), and also allows for the definition of new operations for new types.
  • Multiple ways to convert a value (ocaml-rs has a 1-on-1 mapping between Rust and OCaml values, in ocaml-interop there is more flexibility and control on what can convert into what).

What ocaml-rs solves for ocaml-interop

  • Derivers for structs/enums (ocaml-interop uses macros to define how to map between Rust structs/enums and OCaml records/variants)
  • Support for custom blocks.
  • Support for bigarrays.
  • Rust panic handling
  • OCaml exceptions from the Rust side, handling Err results to produce OCaml exceptions
  • Annotations for exported functions (ocaml-interop uses a wrapper macro for this)
  • Support for bytecode OCaml

Ideas for the future

  • Starting from the current ocaml-rs derivers, and combined with ocaml-interop's representation of OCaml values, implementations for interacting with OCaml records/variants could be generated for derive annotated structs so that allocations that result from OCaml->Rust conversions can be avoided, or controlled with more granularity.

Modifications Summary

Additions

WIP

  • ocaml-sys: a macro like ocaml_frame from ocaml-interop but for plain Values to replace caml_param?

Changes

WIP

  • ocaml-rs: Value would become a re-export of ocaml-interops OCaml<'a, T>.
  • ocaml-rs: Exported functions would have an implicit ocaml_frame instead of an caml_body.

Deprecations / Removals

WIP

  • ocaml-sys: caml_param and caml_body macros. ocaml_frame macro from ocaml-interop should be used instead (maybe a lower-level version that works using RawOCaml values could be implemented and added to ocaml-sys).
  • ocaml-rs: List, Array, etc types get replaced by OCaml<OCamlList<T>>, etc types from ocaml-interop.
  • ocaml-interop: most (all?) macros related to mapping between Rust<->OCaml Structs/Enums/RecordsVariants. Not needed when the ocaml-rs derivers exist (doesn't cover all the cases that the macros cover, but those are probably better handled by intermediary conversions anyway).
  • ocaml-interop: ocaml_export macro, not needed when the function annotations from ocaml-rs exist.

Other

  • Would be great if the OCaml version changes fragility situation could be improved somehow (ocaml-rs does a good job already, but it core OCaml could help us here, that would be great, because the OCaml runtime seems to be going through a bunch of changes lately).
  • At the moment, no synchronization is made by ocaml-interop (nor ocaml-rs?) for accessing the OCaml runtime, and is left for the user to handle. Having a solution baked into the libraries would be good.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions