Show HN: Hsrs – Type-Safe Haskell Bindings Generator for Rust (github.com)

53 points by suis_siva ↗ HN
Hey everyone! I've been working on hsrs, a type-safe Haskell Bindings Generator for Rust.

I couldn't really find any bindings generator that would create type-safe, rich bindings for Haskell from Rust. Naturally, both languages have rich type systems, so I was amazed that no awesome bindings generator already existed, hence I decided to write my own. hsrs feels very similar to pyo3 and napi-rs, and if you've used those, hsrs will feel right at home.

What's unique about hsrs as opposed to hs-bindgen is that it has type-safe bindings for rich types, like Result, Maybe, etc. while also generating Haskell bindings. The repo contains a minimal example, and more details are available in the haskell discourse: https://discourse.haskell.org/t/ann-hsrs-ergonomic-haskell-b...

7 comments

[ 7.1 ms ] story [ 22.2 ms ] thread
Really cool nice job!

With more and more ecosystem libraries being written in Rust (and a Python wrapper) could this be a way to expand the Haskell ecosystem?

It would be so nice if these "well typed languages" would be able to interact better with each other. Alas, the GC will probably cause some friction, e.g. the ocaml-rs package makes this pretty visible: https://github.com/zshipko/ocaml-rs/blob/master/examples/rus... . (I have not used either of these projects.)

Maybe the GC would be less of a problem if we didn't want to have the best possible performance as well.

It's great that this allows passing Rust structs both as ForeignPtr and as native records with marshalling!

Some questions/ideas:

- Is there a way to generate #[hsrs::data_type] bindings for Rust library types, or do you need to create custom wrapper types for them?

- It seems like all #[hsrs::function]s are translated to return IO (since Rust functions can do arbitrary side effects). It would be great if you could (unsafely) mark functions as pure, to get pure Haskell functions without having to wrap them in unsafePerformIO.

- Both Haskell and Rust have HM type systems, so I wonder if you could also translate type classes from Rust to Haskell.

Nice work. One thing I didn't see covered: what happens when a #[hsrs::function] panics? Unwinding across the FFI boundary into the Haskell RTS is UB, so I assume there's a catch_unwind somewhere converting it into a Haskell exception, or do panics just abort the process?
Ooh! Sorry for the late reply, missed this. If #[hsrs::function]s panic, the Haskell runtime crashes so everything gets aborted. I wasn't really thinking about this, to be honest, but it's definitely a *very* good point!

I'll put it on the list of things for the next release! Thanks =)