-
Notifications
You must be signed in to change notification settings - Fork 58
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 failable initializers #276
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,4 +220,11 @@ class OptionTests: XCTestCase { | |
XCTAssertEqual(reflectedSome!.field, 123) | ||
XCTAssertNil(reflectedNone) | ||
} | ||
|
||
/// Verify that we can use failable initializers defined on the Rust side. | ||
func testFailableInitializer() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From now on all tests should say what we're verifying.
This helps:
For simple stuff like this a sentence is fine. |
||
XCTAssertEqual(FailableInitType(false), nil) | ||
let failableInitType = FailableInitType(true) | ||
XCTAssertEqual(failableInitType!.count(), 132) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -456,3 +456,74 @@ typedef struct MyType MyType; | |
.test(); | ||
} | ||
} | ||
|
||
/// Verify that we generated a Swift class with a failable init method. | ||
mod extern_rust_class_with_failable_init { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @chinedufn Related: #276 (comment) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good |
||
use super::*; | ||
|
||
fn bridge_module_tokens() -> TokenStream { | ||
quote! { | ||
mod foo { | ||
extern "Rust" { | ||
type Foo; | ||
|
||
#[swift_bridge(init)] | ||
fn new() -> Option<Foo>; | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn expected_rust_tokens() -> ExpectedRustTokens { | ||
ExpectedRustTokens::Contains(quote! { | ||
# [export_name = "__swift_bridge__$Foo$new"] | ||
pub extern "C" fn __swift_bridge__Foo_new () -> * mut super :: Foo { | ||
if let Some (val) = super :: Foo :: new () { | ||
Box :: into_raw (Box :: new (val)) | ||
} else { | ||
std :: ptr :: null_mut () | ||
} | ||
} | ||
}) | ||
} | ||
|
||
const EXPECTED_SWIFT: ExpectedSwiftCode = ExpectedSwiftCode::ContainsAfterTrim( | ||
r#" | ||
public class Foo: FooRefMut { | ||
var isOwned: Bool = true | ||
|
||
public override init(ptr: UnsafeMutableRawPointer) { | ||
super.init(ptr: ptr) | ||
} | ||
|
||
deinit { | ||
if isOwned { | ||
__swift_bridge__$Foo$_free(ptr) | ||
} | ||
} | ||
} | ||
extension Foo { | ||
public convenience init?() { | ||
guard let val = __swift_bridge__$Foo$new() else { return nil }; self.init(ptr: val) | ||
} | ||
} | ||
"#, | ||
); | ||
|
||
const EXPECTED_C_HEADER: ExpectedCHeader = ExpectedCHeader::ContainsAfterTrim( | ||
r#" | ||
void* __swift_bridge__$Foo$new(void); | ||
"#, | ||
); | ||
|
||
#[test] | ||
fn extern_rust_class_with_failable_init() { | ||
CodegenTest { | ||
bridge_module: bridge_module_tokens().into(), | ||
expected_rust_tokens: expected_rust_tokens(), | ||
expected_swift_code: EXPECTED_SWIFT, | ||
expected_c_header: EXPECTED_C_HEADER, | ||
} | ||
.test(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need these two
DEVELOPMENT_TEAM
lines? If not lets revert them.