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 missing C headers and Swift code for Vec<f32/f64> support #200

Merged
merged 3 commits into from
Mar 17, 2023

Conversation

rkreutz
Copy link
Contributor

@rkreutz rkreutz commented Mar 17, 2023

This PR adds support for Vec<f32>/Vec<f64> (it seems it was mostly some C headers and Swift code missing).

Rust code would look like:

#[swift_bridge::bridge]
mod ffi {
    #[swift_bridge(swift_repr = "struct")]
    struct SomeStruct {
        field: Vec<f32>,
    }

    extern "Rust" {
        fn do_something(val: SomeStruct) -> Vec<f32>;
    }
}

fn do_something(val: SomeStruct) -> Vec<f32> {
    /* do something */
}

On Swift side, Double and Float are now Vectorizable.

@rkreutz
Copy link
Contributor Author

rkreutz commented Mar 17, 2023

@chinedufn I couldn't find any tests for the other primitives Vec support, so I wasn't sure where to add tests (or if they are necessary). Please advise on how you'd like to proceed.

@chinedufn
Copy link
Owner

chinedufn commented Mar 17, 2023

Thanks!

You can find a basic Vec<Primitive> test here

/// 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
/// functions that generate the pieces of the RustVec support aren't accidentally hard coded to
/// only work for one type.
/// Here we call the rest of the types, confident that if we can construct them then the rest of their
/// methods will work since they worked for the other types above.
func testConstructPrimitiveRustVecs() throws {
XCTAssertEqual(RustVec<UInt8>().pop(), nil);
XCTAssertEqual(RustVec<UInt16>().len(), 0);
XCTAssertEqual(RustVec<UInt32>().len(), 0);
XCTAssertEqual(RustVec<UInt64>().len(), 0);
XCTAssertEqual(RustVec<UInt>().len(), 0);
XCTAssertEqual(RustVec<Int8>().len(), 0);
XCTAssertEqual(RustVec<Int16>().len(), 0);
XCTAssertEqual(RustVec<Int32>().len(), 0);
XCTAssertEqual(RustVec<Int64>().len(), 0);
XCTAssertEqual(RustVec<Int>().len(), 0);
XCTAssertEqual(RustVec<Bool>().len(), 0);
}

@@ -121,21 +127,27 @@ fn vec_of_primitive_headers(rust_ty: &str, c_ty: &str) -> String {

// __private__OptionU8 ... etc
let option_ty = format!("{}{}", "__private__Option", capatilized_first_letter);
let option_ty_tag = if option_ty == "__private__OptionF64" {
Copy link
Owner

Choose a reason for hiding this comment

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

We can delete these changes and change OptionDouble to OptionF64 here

typedef struct __private__OptionF64 { double val; bool is_some; } __private__OptionDouble;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I've thought the same, just wasn't sure if there was a reason for them to not be equal. Anyway, will do this change and add the tests. Cheers

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests 60af50d and renamed OptionDouble to OptionF64 6dd8e42

@rkreutz rkreutz requested a review from chinedufn March 17, 2023 23:22
@chinedufn chinedufn merged commit b4c5d9b into chinedufn:master Mar 17, 2023
@chinedufn
Copy link
Owner

Thanks!!

@rkreutz rkreutz deleted the feature/float-vec-support branch April 8, 2023 22:12
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.

2 participants