What is Craby?
mainObjCTurboModule (iOS) and JavaTurboModule (Android), Craby achieves near-zero overhead for cross-language communication.repository·main·Indexed 42 days ago
https://github.com/leegeunhyeok/crabyA tool for writing type-safe Rust code in React Native applications. Craby automates the creation of bindings between TypeScript and Rust by leveraging pure C++ TurboModules, bypassing platform-specific layers like ObjCTurboModule and JavaTurboModule. It requires React Native 0.76+ with the New Architecture enabled, Node.js 18+, and Rust 1.90+.
ObjCTurboModule (iOS) and JavaTurboModule (Android), Craby achieves near-zero overhead for cross-language communication.ObjCTurboModule (iOS) and JavaTurboModule (Android), streamlining the bridge between Rust and the React Native runtime.@craby/cli-bindings. It is specifically intended for use on Apple Silicon (ARM64) macOS systems.@craby/cli-bindings. It is intended for users running the Craby CLI on Intel-based macOS systems.i686-pc-windows-msvc binary target for @craby/cli-bindings. It is specifically intended for users running on 32-bit Windows environments using the MSVC toolchain.@craby/cli-bindings-linux-x64-musl package provides the pre-compiled x86_64-unknown-linux-musl binary for the @craby/cli-bindings library. Use this specific package if your target environment is a Linux system using the musl C library (common in Alpine Linux or other lightweight container environments) on an x86_64 architecture.Craby is optimized for performance-critical pure computation modules. It is not intended for modules that require direct access to native platform capabilities or standard React Native TurboModule features.
Craby does not support APIs requiring direct native access, including:
Note: App data I/O (File I/O) operations are fully supported.
Craby does not support the following standard TurboModule capabilities:
NativeEventEmitter)Context on Android, UIApplication on iOS)Craby modules use two distinct strategies for error handling depending on whether the operation is synchronous or asynchronous:
Panics: Used for synchronous, immediate errors. When you use the throw! macro in Rust, the panic is caught by std::panic::catch_unwind(), converted into a C++ exception, and finally emitted as a jsi::JSError in the JavaScript runtime. This allows synchronous Rust errors to be caught using standard JavaScript try/catch blocks.
Promise Rejections: Used for recoverable errors in asynchronous operations. Instead of throwing an exception, you return a rejected promise. These are handled in JavaScript using await within a try/catch block or by attaching a .catch() handler to the returned Promise.
Craby automatically converts types between TypeScript, Rust, and C++ at compile-time using zero-cost abstractions. This allows you to define interfaces in TypeScript and implement them in Rust or C++ seamlessly.
| TypeScript | Rust | C++ |
|---|---|---|
boolean | bool | bool |
number | f64 | double |
string | &str (params) / String (others) | std::string |
object | struct | struct |
ArrayBuffer | Vec<u8> | std::vector<uint8_t> |
T[] | Vec<T> | std::vector<T> |
T | null | Nullable<T> | struct |
Promise<T> | Result<T> | T (Unwrapped) |
enum | enum | enum class |
void | () | void |
Craby provides aliases in Rust to make types more familiar to TypeScript developers:
bool $\rightarrow$ Booleanf64 $\rightarrow$ NumberVec<u8> $\rightarrow$ ArrayBufferVec<T> $\rightarrow$ Array<T>Result<T> $\rightarrow$ Promise<T>() $\rightarrow$ VoidSignals are a mechanism for one-way, asynchronous communication from native Rust code to JavaScript. They allow your native module to emit events that trigger callbacks within your React Native application.
Key Characteristics:
Craby provides two execution models for native methods, allowing you to balance performance and UI responsiveness:
Synchronous (Sync): Methods execute immediately on the JavaScript thread. They return values directly (e.g., number, string). Because they run on the JS thread, they block execution until they complete. Use these for lightweight operations that finish in under 16ms (to maintain 60fps).
Asynchronous (Async): Methods return a Promise<T> and execute in separate threads managed by the C++ layer. This prevents the UI from freezing during heavy computations. Use these for any operation that takes longer than 16ms or involves heavy CPU work.
| Aspect | Sync | Async (Promise) |
|---|---|---|
| Execution | JS thread | Separate thread |
| Returns | T | Promise<T> |
| Duration | < 16ms | Any duration |
| Heavy Work | Avoid | Perfect for |
| UI Impact | Blocking | Non-blocking |
| Use Cases | Math, formatting | Heavy compute, complex algorithms |