swift-bridge icon indicating copy to clipboard operation
swift-bridge copied to clipboard

Support failable Initializers for Opaque Types

Open NiwakaDev opened this issue 1 year ago • 5 comments

Something like:

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        type SomeType;
        #[swift_bridge(init)]
        fn new() -> Result<SomeType, String>;
    }
}
do {
   let someType = try SomeType()
} catch let error {
   //..
}

Or

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        type SomeType;
        #[swift_bridge(init)]
        fn new() -> Option<SomeType>;
    }
}
if let someType = SomeType() {
    //...
}

I'd like to use this feature, but I don't know whether or not other users would like to.

NiwakaDev avatar Jul 07 '23 03:07 NiwakaDev

I hadn't heard of this feature. Yeah this sounds like something that we want to support.

https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization/#Failable-Initializers

I'm thinking we should also add a #[swift_bridge(init)] section to the Function Attributes documentation and have that section's bridge module show both regular and failable initializers https://github.com/chinedufn/swift-bridge/blob/27246447b4ac741b8bf086d3dfca15b47558e44f/book/src/bridge-module/functions/README.md?plain=1#L66

chinedufn avatar Jul 07 '23 14:07 chinedufn

Any updates on this? Would love to use failable initializers instead of a static methods that return optionals

nafehshoaib avatar Nov 07 '23 23:11 nafehshoaib

@nafehshoaib

Any updates on this?

Not yet.

Would love to use failable initializers instead of a static methods that return optionals

I guess that I can implement this issue in three weeks if needed.

NiwakaDev avatar Nov 08 '23 03:11 NiwakaDev

@chinedufn Can I implement this feature?

NiwakaDev avatar Apr 22 '24 23:04 NiwakaDev

Yeah that would be great.

I think "Failable Initializers" are ones that return an Option. We can refer to inits that can throw as "Throwing Initializers".

Feel free to implement either or both.

chinedufn avatar Apr 23 '24 07:04 chinedufn

Next, I'll implement throwing initializers. I'd like to use this feature.

NiwakaDev avatar Aug 08 '24 22:08 NiwakaDev

@chinedufn

Can we release a version that includes this feature? I'd like to use it.

NiwakaDev avatar Aug 15 '24 13:08 NiwakaDev

Released in 0.1.57 https://crates.io/crates/swift-bridge/0.1.57

chinedufn avatar Aug 15 '24 13:08 chinedufn

Thank you!!

NiwakaDev avatar Aug 15 '24 13:08 NiwakaDev