$ show mozilla/uniffi-rs
UniFFI is a mature, production-grade multi-language FFI (Foreign Function Interface) bindings generator for Rust, originally developed by Mozilla and used extensively in Firefox mobile (Android and iOS) and desktop. The system parses Rust interfaces either via a WebIDL-like UDL (UniFFI Definition Language) file or through Rust proc-macros (`#[uniffi::export]`, `#[derive(uniffi::Enum)]`), then generates scaffolding Rust code and high-level bindings for Kotlin (both JNA and JNI backends), Swift, Python, and Ruby, with community support for Go, C#, Dart, and others. The architecture is a layered pipeline: first, metadata is extracted from Rust source or UDL files; second, a language-agnostic intermediate representation (IR) is built and transformed (renaming, type resolution, checksum computation); third, language-specific IR is derived and fed into template-based code generators that produce idiomatic foreign code. The system handles complex features including async functions (with Rust futures and foreign futures), callback interfaces (foreign-implemented traits), custom types, external crate types, recursive types, error types with rich variants, and object reference counting via handles. The codebase is organized as a Cargo workspace with over 60 crates, each with clear responsibilities: `uniffi_core` provides the FFI runtime (RustBuffer, ForeignBytes, FfiConverter, async schedulers), `uniffi_macros` handles proc-macro expansion, `uniffi_parse_rs` parses Rust source, `uniffi_udl` parses UDL files via a custom WebIDL parser (`weedle2`), `uniffi_bindgen` orchestrates code generation, and `uniffi_pipeline` provides a generic IR transformation framework.
The LLM identified 6 files worth highlighting for reuse:
Defines `RustFuture` and `Scheduler` for polling async Rust functions across the FFI boundary. Implements a wait-free polling loop with cancellation support via a waker callback mechanism. This is a clean, generic abstraction for exposing Rust async functions to any foreign language, reusable as a standalone pattern for any project needing async FFI.
Implements `RustBuffer` – a safe, trivially copyable byte buffer for transferring ownership of `Vec<u8>` across the FFI boundary. Uses capacity/length/data fields and provides safe constructors and destructors. This is a fundamental, reusable component for any Rust library that needs to pass variable-length data to foreign code without copying.
Provides a generic `Pipeline` struct that chains named transforms (each implementing `Transform` trait) and optionally records intermediate outputs for debugging. Supports CLI options to run only specific passes. This is a well-designed, standalone pipeline framework that can be extracted and reused for any multi-stage transformation workflow.
Defines `MapNode` and `Node` traits for type-safe, recursive IR transformation. `MapNode` allows converting or rewriting all children of an IR node in one pass, while `Node` provides tree traversal and visitor capabilities. The derive macros (`uniffi_internal_macros`) auto-generate implementations. This is a reusable pattern for any codebase that needs composable, typed AST/IR transformations.
Implements the runtime for callback interfaces – Rust traits that can be implemented by foreign code. Uses a vtable of function pointers registered at runtime, with a generic Rust wrapper that dispatches calls through the vtable. Demonstrates a clean, minimal pattern for enabling foreign implementations of Rust traits, reusable for any FFI project.
A complete, zero-dependency WebIDL parser built with custom parser combinators. Handles all standard WebIDL constructs (interfaces, dictionaries, enums, typedefs, extended attributes) and produces a typed AST. This crate is fully independent and can be reused by any project needing to parse WebIDL files.