Skip to content
This repository has been archived by the owner on Dec 4, 2023. It is now read-only.

Commit

Permalink
Merge pull request #11 from eigerco/new-main
Browse files Browse the repository at this point in the history
changes
  • Loading branch information
MeerKatDev authored Oct 26, 2023
2 parents 57fe1a5 + 2779c10 commit 541c579
Show file tree
Hide file tree
Showing 66 changed files with 1,089 additions and 873 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
be used to avoid this and use the specified value.

- Crates can now use proc-macros without UDL files to export their interface. See the "Procedural Macros: Attributes and Derives" manual section for details.

- [Custom Types](https://mozilla.github.io/uniffi-rs/proc_macro/index.html#the-unifficustomtype-derive) are now supported for proc-macros, including a very
low-friction way of exposing types implementing the new-type idiom.
- Proc-macros: Added support for ByRef arguments
- Proc-macros: Implemented custom type conversion error handling (https://mozilla.github.io/uniffi-rs/udl/custom_types.html#error-handling-during-conversion)
- Error types must now implement `Error + Send + Sync + 'static`.

### What's Fixed

Expand Down
87 changes: 10 additions & 77 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ members = [
"uniffi_macros",
"uniffi_meta",
"uniffi_testing",
"uniffi_udl",
"uniffi_docs",
"uniffi_udl",
"uniffi",
"weedle2",

Expand All @@ -23,16 +23,15 @@ members = [
"examples/todolist",
"examples/traits",

"fixtures/benchmarks",
"fixtures/coverall",
"fixtures/callbacks",

"fixtures/ext-types/guid",
"fixtures/ext-types/http-headermap",
"fixtures/ext-types/uniffi-one",
"fixtures/ext-types/lib",
"fixtures/ext-types/proc-macro-lib",

"fixtures/benchmarks",
"fixtures/coverall",
"fixtures/callbacks",
"fixtures/foreign-executor",
"fixtures/keywords/kotlin",
"fixtures/keywords/rust",
Expand Down
57 changes: 19 additions & 38 deletions docs/manual/src/proc_macro/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,45 +91,13 @@ trait MyTrait {

```

Most UniFFI [builtin types](../udl/builtin_types.md) can be used as parameter and return types.
When a type is not supported, you will get a clear compiler error about it.

All owned [builtin types](../udl/builtin_types.md) and user-defined types can be used as arguments
and return types.

Arguments and receivers can also be references to these types, for example:

```rust
// Input data types as references
#[uniffi::export]
fn process_data(a: &MyRecord, b: &MyEnum, c: Option<&MyRecord>) {
...
}

#[uniffi::export]
impl Foo {
// Methods can take a `&self`, which will be borrowed from `Arc<Self>`
fn some_method(&self) {
...
}
}

// Input foo as an Arc and bar as a reference
fn call_both(foo: Arc<Foo>, bar: &Foo) {
foo.some_method();
bar.some_method();
}
```

The one restriction is that the reference must be visible in the function signature. This wouldn't
work:

```rust
type MyFooRef = &'static Foo;

// ERROR: UniFFI won't recognize that the `foo` argument is a reference.
#[uniffi::export]
fn do_something(foo: MyFooRef) {
}
```
User-defined types are also supported in a limited manner: records (structs with named fields,
`dictionary` in UDL) and enums can be used when the corresponding derive macro is used at
their definition. Opaque objects (`interface` in UDL) can always be used regardless of whether they
are defined in UDL and / or via derive macro; they just need to be put inside an `Arc` as always.

## The `uniffi::Record` derive

Expand Down Expand Up @@ -218,6 +186,19 @@ impl Foo {
}
```

Exported functions can input object arguments as either an `Arc<>` or reference.
```rust
// Input foo as an Arc and bar as a reference
fn call_both(foo: Arc<Foo>, bar: &Foo) {
foo.method_a();
bar.method_rba();
```

There are a couple limitations when using references for arguments:
- They can only be used with objects and trait interfaces
- The reference must be visible in the function signature.
If you have a type alias `type MyFooRef<'a> = &'a Foo`, then `fn do_something(foo: MyFooRef<'_>)` would not work.

## The `uniffi::custom_type` and `uniffi::custom_newtype` macros

There are 2 macros available which allow procmacros to support "custom types" as described in the
Expand Down
2 changes: 0 additions & 2 deletions fixtures/coverall/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ fn throw_flat_macro_error() -> Result<(), CoverallFlatMacroError> {
Err(CoverallFlatMacroError::TooManyVariants { num: 88 })
}

#[derive(Debug, thiserror::Error)]
pub enum CoverallRichErrorNoVariantData {
#[error("TooManyPlainVariants")]
TooManyPlainVariants,
}

Expand Down
1 change: 0 additions & 1 deletion fixtures/futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ path = "src/bin.rs"

[dependencies]
uniffi = { path = "../../uniffi", version = "0.24", features = ["tokio", "cli"] }
thiserror = "1.0"
tokio = { version = "1.24.1", features = ["time", "sync"] }
once_cell = "1.18.0"

Expand Down
6 changes: 2 additions & 4 deletions fixtures/futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ pub async fn sleep(ms: u16) -> bool {
}

// Our error.
#[derive(thiserror::Error, uniffi::Error, Debug)]
#[derive(uniffi::Error, Debug)]
pub enum MyError {
#[error("Foo")]
Foo,
}

Expand Down Expand Up @@ -284,9 +283,8 @@ pub struct SharedResourceOptions {
}

// Our error.
#[derive(thiserror::Error, uniffi::Error, Debug)]
#[derive(uniffi::Error, Debug)]
pub enum AsyncError {
#[error("Timeout")]
Timeout,
}

Expand Down
1 change: 0 additions & 1 deletion fixtures/metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ publish = false
name = "uniffi_fixture_metadata"

[dependencies]
thiserror = "1.0"
uniffi = { path = "../../uniffi", version = "0.24" }
uniffi_meta = { path = "../../uniffi_meta" }
uniffi_core = { path = "../../uniffi_core" }
Loading

0 comments on commit 541c579

Please sign in to comment.