|
| 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 | +``` |
0 commit comments