HeadlinesBriefing favicon HeadlinesBriefing.com

Hsrs Generates Type-Safe Rust to Haskell FFI Bindings

Hacker News •
×

A new tool called hsrs aims to simplify calling Rust code from Haskell through type-safe, automatically generated FFI bindings. The generator handles memory management, serialization, and type conversions, producing idiomatic Haskell that feels familiar to developers who have used pyo3 or napi-rs.

hsrs uses Borsh serialization to handle complex types across the language boundary. Rust's Result<T, E> maps to Haskell's Either E T, Option<T> becomes Maybe T, and Vec<T> translates to [T]. The tool provides annotations like #[hsrs::module], #[hsrs::data_type], and #[hsrs::value_type] to control the binding generation process.

Unlike existing solutions like hs-bindgen, hsrs generates type-safe bindings for rich types rather than just basic FFI glue. Developers annotate Rust structs and functions, then run the code generator to produce Haskell modules with proper ForeignPtr management and automatic cleanup.

The project fills a gap in the Haskell ecosystem where safe, ergonomic Rust interop was previously lacking. With MIT/Apache-2.0 licensing and straightforward setup requiring just dependency additions on both sides, hsrs makes polyglot development more accessible.