Skip to content

Haskell-Rust interop via C FFI - #116

Draft
georgefst wants to merge 4 commits into
mainfrom
rust-ffi
Draft

Haskell-Rust interop via C FFI#116
georgefst wants to merge 4 commits into
mainfrom
rust-ffi

Conversation

@georgefst

@georgefst georgefst commented Aug 4, 2026

Copy link
Copy Markdown
Collaborator

This is the end result of my investigations in to how best to call Rust code from Haskell.

The general idea is as follows. We have a Rust crate geomerge-ffi which compiles to a static library. We run cbindgen on this to generate a corresponding header file. Then the new Haskell FFI tool hs-bindgen can act as though it's dealing with a C library, and generate automatic bindings.

What's neat about this approach is that:

  • We get very high performance across the boundary. Nothing is encoded, though for now all data is copied.
  • It really feels like a hybrid Haskell+Rust codebase. For example, we can change a type or name in the Rust code and the Haskell LSP flags the corresponding error in a split second.

While hs-bindgen has improved substantially since I first looked in to it a few months ago, it's still in alpha and there are still some rough edges. No bugs as such that I've found, but there are a lot of open questions as to best practice for using it this way. Note the TODO comments in the main (penultimate) commit.

I'll likely be carrying on these experiments in some other project. For Coln, I suspect that none of the remaining contributors would want to take on the maintenance burden of actually merging this work. Especially since the actual interface between the Coln compiler and the storage engine is likely to involve infrequently passing JSON back and forth between Wasm modules, rather than there being any need for a single native binary. Nevertheless, I see no harm in leaving this here as a proof-of-concept.

In the final commit on this branch, I vibe-coded some JSON serialisation tests which make use of this FFI work, and which have discovered bugs in Coln. I'll merge those via a separate PR. It is cool to be able to test the Rust code from the Haskell test runner, but there are certainly simpler ways to test the JSON encoding consistency than making use of all this machinery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant