Skip to content
Tech News
← Back to articles

Show HN: Hsrs – Type-Safe Haskell Bindings Generator for Rust

read original more articles
Why This Matters

The hsrs tool streamlines the integration of Rust and Haskell by providing type-safe, automatically generated FFI bindings. This simplifies cross-language communication, enhances safety, and reduces boilerplate, making it easier for developers to leverage Rust's performance with Haskell's expressive type system. As a result, it accelerates development and improves reliability in multi-language projects within the tech industry.

Key Takeaways

hsrs

Call Rust from Haskell with type-safe, automatically generated FFI bindings.

Annotate your Rust types and functions, run the code generator, and get idiomatic Haskell that handles memory management, serialization, and type conversions for you.

Quick start

1. Annotate your Rust code

#[hsrs::module] mod canvas { #[hsrs::value_type] pub struct Point { pub x: i32, pub y: i32, } #[hsrs::data_type] pub struct Canvas { points: Vec<Point>, } impl Canvas { #[hsrs::function] pub fn new() -> Self { Self { points: vec![] } } #[hsrs::function] pub fn add_point(&mut self, p: Point) { self.points.push(p); } #[hsrs::function] pub fn count(&self) -> u64 { self.points.len() as u64 } } }

2. Generate Haskell bindings

cargo install hsrs-codegen hsrs-codegen src/lib.rs -o Bindings.hs

3. Use from Haskell

import Bindings main :: IO () main = do c <- new addPoint c (Point 10 20) n <- count c print n -- 1

... continue reading