ocaml-interop icon indicating copy to clipboard operation
ocaml-interop copied to clipboard

allow to_ocaml to take owned self

Open tiny-book2 opened this issue 1 year ago • 4 comments

This is a rough proposal (with code!) to update ToOCaml::to_ocaml to take self instead of &self. It also changes many default implementations of ToOCaml to be implemented on &SomeType instead of SomeType. As far as I understand it:

  • this usually won't affect the ergonomics of calling to_ocaml, since Rust will automatically take a reference of when calling a method on it.
  • if you're implementing a generic function, and want to make sure you can convert just a reference of something into OCaml without giving up ownership, you can still add the type constraint for <'a> &'a T: ToOCaml
  • i think this allows users to implement more efficient conversions in some cases --- for example, perhaps an owned String could be converted into a Bigarray without copying?

tiny-book2 avatar Oct 12 '23 02:10 tiny-book2

(I think this doesn't quite work, but thought I'd at least open the conversation)

tiny-book2 avatar Oct 12 '23 05:10 tiny-book2

Ok, with some extra changes, I think this largely works?? Was able to update polars-ocaml to use it in an experimental branch: https://github.com/mt-caret/polars-ocaml/pull/87

tiny-book2 avatar Oct 12 '23 06:10 tiny-book2

Hi. I have to take a deeper look when I have time, but at first sight I am not very convinced about the change.

i think this allows users to implement more efficient conversions in some cases --- for example, perhaps an owned String could be converted into a Bigarray without copying?

Totally agree that this is useful, but it is not what most Rust->OCaml conversions do right? such cases are rarer and are doing something more delicate by re-using in OCaml memory that lives outside of OCaml's heap, so I would rather have for such cases a different IntoOCaml trait with a well documented (with potential pitfalls) into_ocaml method.

tizoc avatar Oct 13 '23 12:10 tizoc

Thanks for taking a look at my wacky idea! That sounds like a good idea; I'll make it when I get a free moment.

tiny-book2 avatar Oct 13 '23 14:10 tiny-book2