Skip to content

Commit

Permalink
proc-macro: Document the Error derive
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Dec 13, 2022
1 parent 7a24e53 commit b4f784d
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions docs/manual/src/proc_macro/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,59 @@ impl Foo {
}
```

## The `uniffi::Error` derive

The `Error` derive registers a type as an error and can be used on any enum that the `Enum` derive also accepts.
By default, it exposes any variant fields to the foreign code.
This type can then be used as the `E` in a `Result<T, E>` return type of an exported function or method.
The generated foreign function for an exported function with a `Result<T, E>` return type
will have the result's `T` as its return type and throw the error in case the Rust call returns `Err(e)`.

```rust
#[derive(uniffi::Error)]
pub enum MyError {
MissingInput,
IndexOutOfBounds {
index: u32,
size: u32,
}
Generic {
message: String,
}
}

#[uniffi::export]
fn do_thing() -> Result<(), MyError> {
// ...
}
```

You can also use the helper attribute `#[uniffi(flat_error)]` to expose just the variants but none of the fields.
In this case the error will be serialized using Rust's `ToString` trait
and will be accessible as the only field on each of the variants.
For flat errors your variants can have unnamed fields,
and the types of the fields don't need to implement any special traits.

```rust
#[derive(uniffi::Error)]
#[uniffi(flat_error)]
pub enum MyApiError {
Http(reqwest::Error),
Json(serde_json::Error),
}

// ToString is not usually implemented directly, but you get it for free by implementing Display.
// This impl could also be generated by a proc-macro, for example thiserror::Error.
impl std::fmt::Display for MyApiError {
// ...
}

#[uniffi::export]
fn do_http_request() -> Result<(), MyApiError> {
// ...
}
```

## Other limitations

In addition to the per-item limitations of the macros presented above, there is also currently a
Expand Down

0 comments on commit b4f784d

Please sign in to comment.