chinedufn / swift-bridge

swift-bridge facilitates Rust and Swift interop.
https://chinedufn.github.io/swift-bridge
Apache License 2.0
844 stars 62 forks source link

Support 128 bit integers `u128` and `i128` #293

Open chinedufn opened 2 months ago

chinedufn commented 2 months ago

Swift 6 introduced support for 128 bit integers https://github.com/swiftlang/swift-evolution/blob/main/proposals/0425-int128.md

We should begin supporting them in swift-bridge.

For instance, after completing this issue the following should compile:

#[swift_bridge::bridge]
mod ffi {
    extern "Rust" {
        fn rust_unsigned_128(num: u128) -> u128;
        fn rust_signed_128(num: i128) -> i128;
    }

    extern "Swift" {
        fn swift_unsigned_128(num: u128) -> u128;
        fn swift_signed_128(num: i128) -> i128;
    }
}
chinedufn commented 2 months ago

Guide

Here's how to add support for u128 and i128:

Add primitive integration tests

Add new integration test functions for u128 and i128:

https://github.com/chinedufn/swift-bridge/blob/54b9c09337701dd7580010d1d5ef6527b855ee98/crates/swift-integration-tests/src/primitive.rs#L1-L91

https://github.com/chinedufn/swift-bridge/blob/54b9c09337701dd7580010d1d5ef6527b855ee98/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunner/Primitive.swift#L1-L57

https://github.com/chinedufn/swift-bridge/blob/54b9c09337701dd7580010d1d5ef6527b855ee98/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/PrimitiveTests.swift#L11-L33

Primitive enum variants

Add U128 and I128 variants to the BuiltInPrimitive and StdlibType enums:

https://github.com/chinedufn/swift-bridge/blob/d713d36391a09fedc5c1b03cd3a15c814f2f11ef/crates/swift-bridge-ir/src/bridged_type/built_in_primitive.rs#L3-L19

Fix compile time errors

Run cargo check -p swift-bridge-ir and fix all compile time errors.

Ensure integration tests pass

Ensure that the PrimitiveTests now pass:

https://github.com/chinedufn/swift-bridge/blob/54b9c09337701dd7580010d1d5ef6527b855ee98/SwiftRustIntegrationTestRunner/SwiftRustIntegrationTestRunnerTests/PrimitiveTests.swift#L11-L33