Giter Site home page Giter Site logo

Comments (4)

badboy avatar badboy commented on June 14, 2024

You should be able to do that using callbacks or really just traits implemented by foreign languages

from uniffi-rs.

tomadimitrie avatar tomadimitrie commented on June 14, 2024

@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.

badboy avatar badboy commented on June 14, 2024

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.

mhammond avatar mhammond commented on June 14, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.