Skip to content

Commit bcefb29

Browse files
authored
Allow all user-defined types to be renamed via procmacros. (#2661)
Fixes #2632, fixes #2212.
1 parent 21ffde9 commit bcefb29

28 files changed

+540
-67
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
### What's new?
1515

16+
- All user-defined types can now be renamed with the proc-macro `name = "NewName"` attribute (like already supported for methods and constructors) ([#2661](https://github.com/mozilla/uniffi-rs/pull/2661))
1617
- Objects can export the `Ord` trait, allowing such objects to be ordered ([#2583](https://github.com/mozilla/uniffi-rs/pull/2583)).
1718
- Objects can implement external traits ([#2430](https://github.com/mozilla/uniffi-rs/issues/2430))
1819
- Procmacros support `#[uniffi(default)]` on a field or `#[uniffi::export(default(arg_name))]` (ie,

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ members = [
5757
"fixtures/regressions/swift-dictionary-nesting",
5858
"fixtures/regressions/unary-result-alias",
5959
"fixtures/regressions/wrong-lower-check",
60+
"fixtures/rename",
6061
"fixtures/trait-methods",
6162
"fixtures/uitests",
6263
"fixtures/uniffi-fixture-time",

docs/manual/src/proc_macro/enumerations.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,17 @@ public enum MyEnum : UInt8 {
8383
case bar = 4
8484
}
8585
```
86+
87+
## Renaming enums
88+
89+
Enums can be renamed in foreign language bindings using the `name` parameter:
90+
91+
```rust
92+
#[derive(uniffi::Enum)]
93+
#[uniffi(name = "RenamedEnum")]
94+
pub enum MyEnum {
95+
// ...
96+
}
97+
```
98+
99+
See [Renaming](./renaming.md) for more details on renaming functionality.

docs/manual/src/proc_macro/functions.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,5 @@ impl Something {
9393
fn do_something(&self) { }
9494
}
9595
```
96+
97+
See [Renaming](./renaming.md) for more details on all renaming capabilites.

docs/manual/src/proc_macro/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ For more details:
5252
* [Interfaces](./interfaces.md)
5353
* [Functions, constructors, methods](./functions.md)
5454
* [Errors](./errors.md)
55+
* [Renaming](./renaming.md)
5556

5657
## The `uniffi::Object` derive to extend interfaces defined in UDL
5758

docs/manual/src/proc_macro/interfaces.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,8 @@ but also be able to be used when a `MyTrait` is required.
123123

124124
Not all bindings support this.
125125

126+
## Renaming objects
127+
128+
Objects can be renamed in foreign language bindings using the `name` parameter on the export attribute;
129+
See [Renaming](./renaming.md) for details.
130+

docs/manual/src/proc_macro/records.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,17 @@ pub struct OptionalRecord {
3636
```
3737

3838
Most types can have [default values](../types/defaults.md)
39+
40+
## Renaming records
41+
42+
Records can be renamed in foreign language bindings using the `name` parameter:
43+
44+
```rust
45+
#[derive(uniffi::Record)]
46+
#[uniffi(name = "RenamedRecord")]
47+
pub struct MyRecord {
48+
// ...
49+
}
50+
```
51+
52+
See [Renaming](./renaming.md) for more details on all renaming capabilites.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Renaming
2+
3+
UniFFI allows you to rename all callables and user-defined types in the foreign bindings using the `name` attribute.
4+
5+
## Examples
6+
7+
### Functions:
8+
9+
```rust
10+
#[uniffi::export(name = "renamed_function")]
11+
fn function(record: Record) -> Enum {
12+
Enum::Record(record)
13+
}
14+
```
15+
16+
### Records and Enums
17+
18+
```rust
19+
#[derive(uniffi::Record)]
20+
#[uniffi(name = "RenamedRecord")]
21+
pub struct Record {
22+
item: i32,
23+
}
24+
25+
#[derive(uniffi::Enum)]
26+
#[uniffi(name = "RenamedEnum")]
27+
pub enum Enum {
28+
VariantA,
29+
Record(Record),
30+
}
31+
```
32+
33+
### Objects, Traits, and methods
34+
35+
If you are renaming both the object and a callable, you must specify the new name in both the `derive` and the `uniffi::export` macros.
36+
37+
Traits cannot yet be renamed, but trait methods can be renamed as usual.
38+
39+
```rust
40+
#[derive(uniffi::Object)]
41+
#[uniffi(name = "RenamedObject")]
42+
pub struct Object {
43+
value: i32,
44+
}
45+
46+
#[uniffi::export(name = "RenamedObject")]
47+
impl Object {
48+
#[uniffi::constructor(name = "renamed_constructor")]
49+
pub fn new(value: i32) -> Self {
50+
Object { value }
51+
}
52+
53+
#[uniffi::method(name = "renamed_method")]
54+
pub fn method(&self) -> i32 {
55+
self.value
56+
}
57+
}
58+
```
59+
60+
### In the bindings
61+
62+
```python
63+
# Python
64+
record = RenamedRecord(item=42)
65+
result = renamed_function(record)
66+
obj = RenamedObject.renamed_constructor(123)
67+
value = obj.renamed_method()
68+
```
69+
70+
```kotlin
71+
// Kotlin
72+
val record = RenamedRecord(item = 42)
73+
val result = renamedFunction(record)
74+
val obj = RenamedObject.renamedConstructor(123)
75+
val value = obj.renamedMethod()
76+
```
77+
78+
```swift
79+
// Swift
80+
let record = RenamedRecord(item: 42)
81+
let result = renamedFunction(record: record)
82+
let obj = RenamedObject.renamedConstructor(value: 123)
83+
let value = obj.renamedMethod()
84+
```

fixtures/rename/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "uniffi-fixture-rename"
3+
edition = "2021"
4+
version = "0.22.0"
5+
license = "MPL-2.0"
6+
publish = false
7+
8+
[lib]
9+
crate-type = ["lib", "cdylib"]
10+
11+
[dependencies]
12+
uniffi = { workspace = true }
13+
thiserror = "2"
14+
15+
[build-dependencies]
16+
uniffi = { workspace = true, features = ["build"] }
17+
18+
[dev-dependencies]
19+
uniffi = { workspace = true, features = ["bindgen-tests"] }
20+
21+
[features]
22+
ffi-trace = ["uniffi/ffi-trace"]

0 commit comments

Comments
 (0)