7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 1 | ## Introduction |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 2 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 3 | **matrix-rust-sdk** leverages [UniFFI](https://mozilla.github.io/uniffi-rs/) to |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 4 | generate bindings for host languages (eg. Swift and Kotlin). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 5 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 6 | Rust code related with bindings live in the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 7 | [matrix-rust-sdk/bindings](https://github.com/matrix-org/matrix-rust-sdk/tree/main/bindings) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 8 | folder. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 9 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 10 | Developers can expose Rust code to UniFFI using two different approaches: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 11 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 12 | - Using an `.udl` file. When a crate has one, you find it under the `src` folder |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 13 | ([an example is here](https://github.com/matrix-org/matrix-rust-sdk/blob/main/bindings/matrix-sdk-ffi/src/api.udl)). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 14 | - Add UniFFI directivies as Rust attributes. In this case Rust source files |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 15 | (`.rs`) contain attributes related to UniFFI (e.g. `#[uniffi::export]`). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 16 | Attributes are preferred, where applicable. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 17 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 18 | ## Expose Rust definitions to UniFFI |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 19 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 20 | ### Check if the API is already on UniFFI |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 21 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 22 | First of all check if the Rust definition you are looking for exists on UniFFI |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 23 | already. Most of exposed matrix definitions are collected in the crate |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 24 | [matrix-sdk-ffi](https://github.com/matrix-org/matrix-rust-sdk/tree/main/bindings/matrix-sdk-ffi). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 25 | This crate contains mainly small Rust wrappers around the actual Rust SDK (e.g. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 26 | the crate |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 27 | [matrix-sdk](https://github.com/matrix-org/matrix-rust-sdk/tree/main/crates/matrix-sdk)) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 28 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 29 | If the Rust definition is on UniFFI already, you either: |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 30 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 31 | - find it in a `.udl` file like |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 32 | [matrix-sdk-ffi/src/api.udl](https://github.com/matrix-org/matrix-rust-sdk/blob/main/bindings/matrix-sdk-ffi/src/api.udl) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 33 | - see it marked with a proper UniFFI Rust attribute like this |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 34 | `#[uniffi::export]` |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 35 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 36 | ### Adding a missing matrix API |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 37 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 38 | 1. Unless you want to contribute on the crypto side, you probably need to add |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 39 | some code in the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 40 | [matrix-sdk-ffi](https://github.com/matrix-org/matrix-rust-sdk/tree/main/bindings/matrix-sdk-ffi) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 41 | crate. After you find the crate you need to understand which file is best to |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 42 | contain the new Rust definition. When exposing new matrix API often (but not |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 43 | always) you need to touch the file |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 44 | [client.rs](https://github.com/matrix-org/matrix-rust-sdk/blob/main/bindings/matrix-sdk-ffi/src/client.rs) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 45 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 46 | 2. Identify the API to expose in the target Rust crate (typically in |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 47 | [matrix-sdk](https://github.com/matrix-org/matrix-rust-sdk/tree/main/crates/matrix-sdk). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 48 | If you can’t find it, you probably need to touch the actual Rust SDK as well. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 49 | In this case you typically just need to write some code around |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 50 | [ruma](https://github.com/ruma/ruma) (a Rust SDK’s dependency) which already |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 51 | implements most of the matrix protocol |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 52 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 53 | 3. After you got (by finding or writing) the required Rust code, you need to |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 54 | expose to UniFFI. To do that just write a small Rust wrapper in the related |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 55 | UniFFI crate (most of the time is **matrix-sdk-ffi**) you found on step 1. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 56 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 57 | 4. When your new (wrapping) Rust definition is ready, remember to expose it to |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 58 | UniFFI. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 59 | It’s best to do it using UniFFI Rust attributes (e.g. `#[uniffi::export]`). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 60 | Otherwise add the new definition in the crate’s `.udl` file. For the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 61 | **matrix-sdk-ffi** crate the definition file is |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 62 | [api.udl](https://github.com/matrix-org/matrix-rust-sdk/blob/main/bindings/matrix-sdk-ffi/src/api.udl). |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 63 | **Remember**: the language inside a `.udl` file isn’t Rust. To |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 64 | [learn more about how map Rust into UDL read here](https://mozilla.github.io/uniffi-rs/udl_file_spec.html) |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 65 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 66 | ## FAQ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 67 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 68 | **Q**: I wrote my Rust code and exposed it to UniFFI. How can I check if the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 69 | compiler is happy?\ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 70 | **A**: Run `cargo build` in the crate you touched (e.g. matrix-sdk-ffi). The |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 71 | compiler will complain if the Rust code and/or the `.udl` is wrong. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 72 | |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 73 | **Q**: The compiler is happy with my code but the CI is failing on GitHub. How |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 74 | can I fix it?\ |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 75 | **A**: The CI may fail for different reasons, you need to have a look on the |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 76 | failing GitHub workflow. One common reason though is that the linter |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 77 | ([Clippy](https://github.com/rust-lang/rust-clippy)) isn’t happy with your code. |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 78 | If this is the case, you can run `cargo clippy` in the crate you touched to see |
7ff8adafe9e4 | atagen | 2026-10-22 12:30 UTC | 79 | what’s wrong and fix it accordingly. |