Comments (4)
You should be able to do that using callbacks
or really just traits implemented by foreign languages
from uniffi-rs.
@badboy that might work, but I have something like this in mind:
I am building an UI framework, so let's take a Button for example (I'm on mobile right now so apologies for potential compilation errors):
#[derive(uniffi::Record)]
struct Button {
on_click: fn()
}
#[uniffi::export]
fn app() -> Button {
Button {
on_click: || {}
}
}
Swift will call the app()
function, parsing the result and displaying a UIButton
or whatever. When the native button is pressed, I need to call the Rust implementation of the handler (the struct field).
Yes, I know I can use an Object for this, but the idea is that the handler should be provided by the user. I was also thinking of this:
// make Button an Object instead of a Record
#[uniffi::export]
impl Button {
fn on_click(&self) {
(self.on_click)();
}
}
But I was wondering whether there is a better solution for this
from uniffi-rs.
If the on_click
callback is implemented on the Rust side anyway, there's no need to pass it over to the foreign-language side.
In that case an object is the better way to handle this.
from uniffi-rs.
In addition to what @badboy says, the more general solution here is to use a trait
- ie, imagine instead of struct Button
you had trait Button
and the foreign code could supply an implementation of the trait. Thus, either Rust or the foreign code could supply an implementation of on_click()
. callback interfaces are just a special case of traits, which is partially why we would recommend using regular traits with foreign bindings rather than callbacks.
from uniffi-rs.
Related Issues (20)
- Using module_name or package_name with external types causes invalid code to be generated. HOT 6
- Can the internal `FFIConverter*` types be hidden? HOT 2
- Documented attribute macro `uniffi::method` is missing HOT 2
- enum/error types are ambiguous HOT 10
- Instantiating AndroidSystemCleaner causes crash on Android 13
- `Lift` / `LiftRef` error with bad compilation error for large enums HOT 3
- Allow foreign implementations of PartialEq, Eq, Hash etc traits HOT 5
- UniFFI generated Swift code fails to compile "Swift 6" mode - due to mutation of shared state.
- uniffi-rs 0.27 throw error with Constructor return type must be Arc<Self> HOT 20
- [tests]: error: consecutive declarations on a line must be separated by ';' HOT 6
- v0.27.0 regression of `uniffi-bindgen generate` HOT 4
- Syntax issues with the generated binding code in Swift HOT 5
- [User Guide] Make Tutorial section self-contained and self-explanatory HOT 1
- Conditional Compilation Error in Kotlin Bindings for Rust Enum with message Field
- Python type aliases don't use forward references HOT 9
- Equality operator for `Object` in Python HOT 2
- Custom type names aren't treated as real type names. HOT 1
- Add support for Rust bindings HOT 5
- Error type named `Error` emits invalid Swift code HOT 6
- Deriving `uniffi::Record` together with `ZeroizeOnDrop` does not compile. Solvable? HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uniffi-rs.