swift-bridge
swift-bridge copied to clipboard
Support failable Initializers for Opaque Types
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.
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
Any updates on this? Would love to use failable initializers instead of a static methods that return optionals
@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.
@chinedufn Can I implement this feature?
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.
Next, I'll implement throwing initializers. I'd like to use this feature.
@chinedufn
Can we release a version that includes this feature? I'd like to use it.
Released in 0.1.57
https://crates.io/crates/swift-bridge/0.1.57
Thank you!!