You'll then need to add some content to the Cargo.toml.
First add the subcrates of arti you want to use to the `[dependencies]` section. You'll have to add `features=["static"]` to crates that support this feature
(at the moment tor-rtcompat and tor-dirmgr): otherwise they will fail either to compile or to run.
(at the moment tor-rtcompat, tor-dirmgr and arti-client): otherwise they will fail either to compile or to run.
You'll also need to add `jni`, to allow Rust and the Java in your app to work together.
```toml
...
...
@@ -67,9 +67,9 @@ You should be familiar with this if you used the JNI before. If not, it's proba
At the moment of writing this guide, Arti does not have a stable Rust API yet. For that reason, no proper bindings are provided.
You'll need to write these bindings yourself by leveraging Rust FFI for C.
There are also rough edges, which will hopefully get polished over time. Most of these should be explained below.
This guide assumes you already have installed Cargo and XCode (but not that you used both together).
Apple requires to have MacOS installed to develop iOS apps, this guide won't work for Linux, Windows or other BSDs.
## Installing the requiremments
First install targets so Rust know how to compile to iOS
```sh
$ rustup target add aarch64-apple-ios \
aarch64-apple-ios-sim \
x86_64-apple-ios
```
## Configuring a Rust project
To create a new project in the directory you're in. You can do:
```sh
$ cargo init <project-name> --lib
```
You'll then need to add some content to the Cargo.toml.
First add the subcrates of arti you want to use to the `[dependencies]` section. You'll have to add `features=["static"]` to crates that support this feature
(at the moment tor-rtcompat, tor-dirmgr and arti-client): otherwise they will fail either to compile or to run.
You'll probably want to add some other dependencies, like futures, but these are not technically requirements.
You'll also need to specify what kind of lib this is. By default, it's a Rust lib that can only be used in the rust ecosystem.
We want it to be a static library:
```toml
[lib]
name="arti_mobile"
crate-type=["staticlib"]
```
You are good to start programming in `src/lib.rs`.
To make your functions available to Swift, you need to set certain modifiers on them.
```rust
// defined the function my_function which will be exported without mangling its name, as a C-compatible function.
.init();// this must be called only once, otherwise your app will probably crash
```
You should take great care about your rust code not unwinding into Swift Runtime: If it does, it will crash your app with no error message to help you.
If your code can panic, you should use `catch_unwind` to capture it before it reaches Swift.
## Async and Swift
Arti relies a lot on Rust futures. There is no easy way to use these futures from Swift. The easiest ways is probably to block on futures
if you are okay with it. Otherwise you have to pass callbacks from Swift to Rust, and make so they are called when the future completes.
Eventually, Arti will provide a set of blocking APIs for use for embedding;
please get in touch if you want to help design them.