Type-safe Rust FFI for React Native
Hey Rustaceans! π
React Native traditionally only supports native modules written in C++, Kotlin/Java (Android), or Swift/Objective-C (iOS). I wanted to use Rust instead, so I built Craby - a tool that brings Rust to React Native with near zero-cost FFI.
Key points:
- Zero-cost FFI: Uses cxx for direct Rust β C++ communication
- Auto-generated bindings: Define your API in TypeScript, implement in Rust - all FFI code is generated
- Compile-time type safety: TypeScript β Rust β C++ types are validated at compile time
- Fast: ~20x faster than standard approaches(aka TurboModule) in benchmarks (5.32ms vs 116ms for 100k calls)
Example:
// TypeScript
interface Spec extends NativeModule {
add(a: number, b: number): number;
}
// Rust
#[craby_module]
impl CalculatorSpec for Calculator {
fn add(&mut self, a: Number, b: Number) -> Number {
a + b // No FFI boilerplate needed!
}
}
This project wouldn't have been possible without Rust's C/C++ interoperability and the amazing cxx project. Huge thanks to that ecosystem.
If you're interested, check out the repository. Thanks for reading!