FFI Arena: Is UniFFI Still the Top Dog?
Can we objectively tell which framework is the best?
On this page
Contestants
This article compares the following FFI frameworks:
Type Coverage
Types used in FFI generally fall into two categories:
- types with a stable representation (e.g.,
[u32; 4]) - types with an unstable representation (e.g.,
Vec<u32>)
The first category requires no conversion glue and is more or less well supported by all frameworks.
The second category requires lowering into a stable counterpart which safer_ffi and Interoptopus choose not to implement. This heavily constrains the API the two can expose over the boundary, but as we will see, this is a safer choice. UniFFI, BoltFFI and Diplomat try to give broader API support but, in doing so, resort to syntactical type resolution.
CO3 supports both type categories. Disabling repr-rust feature constrains the API to the first category only.
Syntactic vs. Semantic Type Resolution
UniFFI, BoltFFI, Diplomat and CO3 can export the following function:
pub fn send(buf: &[u8]) {}
Now let’s see what happens when we switch to a type alias:
pub type Buffer = [u8];
#[cfg_attr(feature = "uniffi", uniffi::export)]
#[cfg_attr(feature = "boltffi", boltffi::export)]
pub fn send(buf: &Buffer) {}
#[cfg(feature = "diplomat")]
#[diplomat::bridge]
pub mod diplomat_case {
use super::Buffer;
pub fn send(buf: &Buffer) {}
}
#[cfg(feature = "co3")]
co3::ffi! {
#![unsafe(export("C"))]
fn send(buf: &Buffer);
}
Only CO3 preserves the behavior.
BoltFFIfails compilation.UniFFIcompiles but changes the ABI and copies the bytes instead of borrowing them.Diplomatcompiles but exposes the Rust slice directly in its C export, silently introducing UB.
Where others resort to syntactical type resolution (based on the name of the type), CO3 resolves types semantically (using the Rust compiler). Given that this is a structural shortcoming, resolving it would require a complete restructuring of each framework’s type resolution. I wouldn’t expect this gap to close any time soon.
If you don’t think that’s a big deal, just consider that a framework with syntactical type resolution can sometimes compile but produce a different lowered type or conversion behavior. Using such a framework is no longer just a matter of limited expressivity or ergonomics. It becomes unpredictable and dangerous.
safer_ffi and Interoptopus outright reject repr(Rust) types as do stabby and abi_stable (if you’re more familiar with them). They don’t pretend to know how to implement them.
Beating the Drum
So how’s full semantic type resolution even possible? Well, I talked about this previously in the release article. In short, CO3 categorizes Rust types according to their relevant properties (with rust-spec) and then dispatches implementations based on those properties with disjoint_impls.
How CO3 does its magic boils down to the following simplified example:
// disjoint_impls augments the Rust compiler's trait resolver
// to recognize impls disjoint on associated types.
disjoint_impls! {
pub trait ReprC {
type CType: ?Sized;
}
impl<'a, R: ReprC + ?Sized> ReprC for &'a mut R
where
// This bound can't be true for both impls at the same time.
R: RustSpec<Size: Thin>,
{
type CType = RefMut<'a, R::CType>;
}
impl<'a, R: ReprC + ?Sized> ReprC for &'a mut R
where
// Both `&mut Buffer` and `&mut [u8]` take this path.
R: RustSpec<Size = MetaSized<SliceLike>>,
{
type CType = WideRefMut<'a, R::CType>;
}
}
A RustSpec implementation describes a type in an n-dimensional space whose axes are properties like Layout, Size, etc. By placing mutually exclusive bounds on those associated types, the ReprC implementations for &mut R each target a disjoint region of that space.
Expanding on this principle, CO3 can precisely target any specific type family at compile time.
House of Cards
The other frameworks also struggle with several other things. Without going into too much detail, I’ll mention a few:
- Niche optimization —
CO3,safer_ffiandInteroptopusapply it consistently. Others don’t. &mut Tforeign writes —CO3alone checks a foreign write on a non-robust cloned value for traps.- Interior mutability —
CO3alone accepts&UnsafeCell<u32>as an exported function parameter. - Custom DSTs —
CO3alone supports custom struct definitions with a DST tail. Others reject them.
The full comparison can be found here.
Binding Existing C Symbols
Of the six frameworks, only CO3 can bind functions from an existing C library.
CO3 does this through the same conversion API that is used for exports, which I also discussed in another article. Given a C library that provides a fill symbol, we can import it by name and have CO3 convert the arguments:
pub type Buffer = [u8];
#[cfg(feature = "co3")]
co3::ffi! {
#![unsafe(extern("C"))] // Notice the switch to `extern`
#[symbol_name = "fill"]
fn fill(buf: &mut Buffer);
}
I would be remiss not to mention that Interoptopus also lets Rust call foreign code through its plugin interface. This, however, does not enable the import of existing C bindings.
Foreign Language Integration
Being more mature, other frameworks offer immediate integration with other languages.
On the other hand, CO3 establishes a separation of concerns through the SIR specification, which foreign-language plugins can be implemented against. Rust-to-Rust and C-to-Rust solutions exist today.
To the extent that CO3 becomes a more established crate in the ecosystem, you should see more plugins emerge.
Stable Intermediate Representation
SIR stands between the bindings exported by CO3 and foreign binding generators. It is a specification that tells each generator how to rebuild the exact exported Rust API in its target language.
CO3 lowers exported Rust declarations to a stable ABI while preserving all relevant type-system information. Each language can write its own generator independently by following the SIR specification.
But CO3 Is Missin’ This or That
Yes, CO3 is missing support for async functions and trait objects … currently.
If there is one key takeaway point from this article, it is that CO3 can and will close these gaps, whereas others can’t break out of syntactical type resolution for repr(Rust) types or custom DSTs easily. Instead, what I can see happening in the future is frameworks accepting CO3 as a dependency that provides them with the type resolution mechanism they can build upon.
The Verdict
I don’t consider this to be a comprehensive comparison of frameworks, but a starting point when deciding which framework to use. I advise you to look into each framework’s documentation before making a final decision.
Being the maintainer of CO3, although I believe I have presented a strong case in this article, I am at significant risk of affecting the verdict with my own biases when I say that CO3’s foundations are much stronger on several different fronts. What it currently lacks is support for generating bindings in other languages.
The verdict is left up to the reader.