uniffi-rs/examples
Jan-Erik Rediger 7f58021c08 Remove outdated comment in example script 2023-08-10 16:21:21 +02:00
..
app Remove outdated comment in example script 2023-08-10 16:21:21 +02:00
arithmetic Add a version to the example/fixture dependencies on uniffi 2023-06-23 12:10:07 +02:00
callbacks Add a version to the example/fixture dependencies on uniffi 2023-06-23 12:10:07 +02:00
custom-types Add a version to the example/fixture dependencies on uniffi 2023-06-23 12:10:07 +02:00
futures New example and docs for Futures (#1634) 2023-07-19 11:30:19 -04:00
fxa-client Correct minor typos (#1432) 2022-12-19 09:45:16 -10:00
geometry Add a version to the example/fixture dependencies on uniffi 2023-06-23 12:10:07 +02:00
rondpoint Remove almost all references to `DOMString` (#1659) 2023-07-24 10:05:38 -04:00
sprites Add a version to the example/fixture dependencies on uniffi 2023-06-23 12:10:07 +02:00
todolist Add a version to the example/fixture dependencies on uniffi 2023-06-23 12:10:07 +02:00
traits Add a version to the example/fixture dependencies on uniffi 2023-06-23 12:10:07 +02:00
README.md Make `uniffi` the one crate that consumers need to depend on 2023-01-12 15:16:19 -05:00

README.md

Example uniffi components

This directory contains some small example components implemented using uniffi. It's currently being used more as a living test environment than user-facing docs, but hopefully it gives you a bit of an idea of what we're up to with this crate.

Newcomers are recommended to explore them in the following order:

  • ./arithmetic/ is the most minimal example - just some plain functions that operate on integers, and some minimal error handling.
  • ./geometry/ shows how to use records and nullable types for working with more complex data.
  • ./sprites/ shows how to work with stateful objects that have methods, in classical object-oriented style.
  • ./todolist is a simplistic todo-list that can only add items and show the last item, meant to show how interacting with strings works.
  • ./rondpoint exercises complex data types by round-tripping them from the foreign-language code, through rust and back again.
  • ./fxa-client doesn't work yet, but it contains aspirational example of what the UDL might look like for an actual real-world component.

Each example has the following structure:

  • src/<namespace>.udl, the component interface definition which defines the main object and its methods. This is processed by functions in build.rs to generate Rust scaffolding for the component.
  • src/lib.rs, the core implementation of the component in Rust. This basically pulls in the generated Rust scaffolding via include!() and fills in function implementations.
  • Cargo.toml configures the crate to build a cdylib with an appropriate name, matching the namespace defined in the UDL file.
  • Some small test scripts that double as API examples in each target foreign language:
    • Kotlin tests/bindings/test_<namespace>.kts
    • Swift tests/bindings/test_<namespace>.swift
    • Python tests/bindings/test_<namespace>.py

If you want to try them out, you will need:

We publish a docker image that has all of this dependencies pre-installed, if you want to get up and running quickly.

With that in place, try the following:

  • Run cargo build. That compiles the component implementation into a native library named uniffi_<namespace> in ../target/debug/.
  • Run cargo test. This will run each of the foreign-language testcases against the compiled Rust code, confirming whether everything is working as intended.
  • Explore the build process in more detail:
    • Run cargo run -p uniffi -- scaffolding ./src/<namespace>.udl. This will generate the Rust scaffolding code which exposes a C FFI for the component. You can view the generatd code in ./src/<namespace>.uniffi.rs.
    • Run cargo run -p uniffi -- generate --language kotlin ./src/<namespace>.udl. This will generate the foreign-language bindings for Kotlin, which load the compiled Rust code and use the C FFI generated above to interact with it. You can view the generated code in ./src/uniffi/<namespace>/<namespace>.kt.
    • Try using --language swift or --language python to explore the foreign-language bindings generated for other languages.