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

Adding Vec support for transparent structs #199

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ default = []
# Enables bridging of async functions.
async = ["tokio", "once_cell"]

# Prioritises compatibility over performance
compatibility = ["swift-bridge-macro/compatibility"]

[build-dependencies]
swift-bridge-build = {version = "0.1.51", path = "crates/swift-bridge-build"}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,34 @@ class VecTests: XCTestCase {
XCTAssertEqual(reflected.get(index: 0)!, TransparentEnumInsideVecT.VariantB)
XCTAssertEqual(reflected.pop()!, TransparentEnumInsideVecT.VariantB)
}

/// Verify that a Vec<T> of transparent struct with derive(Copy, Clone) can be used
/// as an argument and return type for extern "Rust" functions.
func testReflectVecOfTransparentStructFromCopy() throws {
let vec: RustVec<TransparentStructInsideVecTWithCopy> = RustVec()
vec.push(value: TransparentStructInsideVecTWithCopy(integer: 10))

let reflected = rust_reflect_vec_transparent_struct_with_copy(vec)
XCTAssertEqual(reflected.len(), 1)
XCTAssertEqual(reflected.get(index: 0)!.integer, 10)
let popped = try XCTUnwrap(reflected.pop())
XCTAssertEqual(popped.integer, 10)
}

/// Verify that a Vec<T> of transparent struct with derive(Clone) can be used as an
/// argument and return type for extern "Rust" functions.
func testReflectVecOfTransparentStructFromClone() throws {
let vec: RustVec<TransparentStructInsideVecT> = RustVec()
vec.push(value: TransparentStructInsideVecT(string: "string".intoRustString(), integer: 10))

let reflected = rust_reflect_vec_transparent_struct(vec)
XCTAssertEqual(reflected.len(), 1)
XCTAssertEqual(reflected.get(index: 0)!.string.toString(), "string")
XCTAssertEqual(reflected.get(index: 0)!.integer, 10)
let popped = try XCTUnwrap(reflected.pop())
XCTAssertEqual(popped.string.toString(), "string")
XCTAssertEqual(popped.integer, 10)
}

/// Verify that we can construct a RustVec of every primitive type.
/// We tested all of the methods on two different primitives above to be sure that our
Expand Down
5 changes: 5 additions & 0 deletions crates/swift-bridge-ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ description = "Holds the data structures and logic for bridge module parsing and
repository = "https://github.com/chinedufn/swift-bridge"
license = "Apache-2.0/MIT"

[features]

# Prioritises compatibility over performance
compatibility = []

[dependencies]
proc-macro2 = "1"
quote = "1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension __swift_bridge__$SomeStruct {
}

fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::ExactAfterTrim(
ExpectedCHeader::ContainsAfterTrim(
r#"
#include <stdint.h>
#include <stdbool.h>
Expand Down Expand Up @@ -680,15 +680,15 @@ func some_function(_ arg: Optional<SomeStruct>) -> Optional<SomeStruct> {
}

fn expected_c_header() -> ExpectedCHeader {
ExpectedCHeader::ExactAfterTrim(
ExpectedCHeader::ContainsManyAfterTrim(vec![
r#"
#include <stdint.h>
#include <stdbool.h>
typedef struct __swift_bridge__$SomeStruct { uint8_t field; } __swift_bridge__$SomeStruct;
typedef struct __swift_bridge__$Option$SomeStruct { bool is_some; __swift_bridge__$SomeStruct val; } __swift_bridge__$Option$SomeStruct;
struct __swift_bridge__$Option$SomeStruct __swift_bridge__$some_function(struct __swift_bridge__$Option$SomeStruct arg);
"#,
)
"#,
"struct __swift_bridge__$Option$SomeStruct __swift_bridge__$some_function(struct __swift_bridge__$Option$SomeStruct arg);",
])
}

#[test]
Expand Down
Loading