Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support returning Result<(), TransparentEnum> from Rust async functions. #189

Conversation

NiwakaDev
Copy link
Collaborator

This PR adds support for returning Result<(), TransparentEnum> from Rust async functions.

Here's an example of using this:

//Rust
#[swift_bridge::bridge]
mod ffi {
    enum SomeEnum {
       //...
    }
    extern "Rust" {
        async fn some_function() -> Result<(), SomeEnum>;
    }
}
//Swift
do {
    let value = try await some_function()
    //...
} catch let error as SomeEnum {
    //...
} catch {
    //...
}

@NiwakaDev
Copy link
Collaborator Author

NiwakaDev commented Mar 7, 2023

Note that This PR doesn't support returning Result<TransparentEnum, ()> from Rust async functions. However, I guess that nobody would use it. If needed, I'll add it to this PR.

@NiwakaDev
Copy link
Collaborator Author

NiwakaDev commented Mar 7, 2023

If this is merged, I guess that we can implement #168.

@chinedufn
Copy link
Owner

chinedufn commented Mar 7, 2023

However, I guess that nobody would use returning Result<TransparentEnum, ()> from Rust async functions.

Yeah I totally agree.

It's unlikely that someone will run into this signature and if they do they can open an issue and use #[swift_bridge(return_with = ...)] to convert it into an Option<TransparentEnum> in the meantime.

If this is merged, I guess that we can implement #168.

Yeah!! Then we can finally address this and make it a Result<Option<TransparentStruct>, reqwest::Error>> or something like that

// TODO: Return a `Result<MyIpAddress, SomeErrorType>`
// Once we support returning Result from an async function.
async fn get_my_ip_from_rust() -> ffi::MyIpAddress {

Copy link
Owner

@chinedufn chinedufn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. Very nice. Thanks for implementing this.

Looks like your custom result implementation is holding up well since adding this support only took like 3 extra lines of code (excluding all of the tests),

Nice!

@chinedufn chinedufn merged commit 2d60d9e into chinedufn:master Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants