Skip to content

0.1.51

Compare
Choose a tag to compare
@chinedufn chinedufn released this 28 Feb 01:13
· 45 commits to master since this release
832498a
  • Support returning Result<(), OpaqueRustType> from Rust functions. #180 (thanks @jfaust)

    // For example, the following is now possible:
    
    #[swift_bridge::bridge]
    mod ffi {
        struct UnitStruct;
    
        extern "Rust" {
            type OpaqueType;
    
            fn null_result() -> Result<(), OpaqueType>;
            fn unit_result() -> Result<UnitStruct, OpaqueType>;
        }
    }
    
    fn null_result() -> Result<(), OpaqueType> {
        Ok(())
    }
    
    fn unit_result() -> Result<UnitStruct, OpaqueType> {
        Ok(UnitStruct)
    }
  • Support shared enums with named data. #175 (thanks @NiwakaDev)

    // For example, the following is now possible:
    
    #[swift_bridge::bridge]
    mod ffi {
        enum EnumWithNamedData {
            Variant1 { hello: String, data_u8: u8 },
            Variant2 { data_i32: i32 },
            Variant3 { foo: Foo },
        }
    
        extern "Rust" {
            #[swift_bridge(Equatable)]
            type Foo;
    
            #[swift_bridge(init)]
            fn new() -> Foo;
    
            fn reflect_enum_with_named_data(arg: EnumWithNamedData) -> EnumWithNamedData;
        }
    }
    // Swift
    
    let enumWithNamedData3 = EnumWithNamedData.Variant3(foo: Foo())
    switch reflect_enum_with_named_data(enumWithNamedData3) {
    case .Variant3(let foo):
        XCTAssertEqual(foo, Foo())
        break
    default:
        XCTFail()
    }
  • Support opaque types inside of data-carrying enums. #181 (thanks @NiwakaDev)

    // For example, the following is now possible:
    
    #[swift_bridge::bridge]
    mod ffi {
        enum OpaqueRustEnum {
            Unnamed(MyType)
            Named { field: MyType },
        }
    
        extern "Rust" {
            #[swift_bridge(Equatable)]
            type MyType;
    
            #[swift_bridge(init)]
            fn new() -> MyType;
    
            fn reflect_enum(arg: OpaqueRustEnum) -> OpaqueRustEnum;
        }
    }
    // Swift
    
    let opaqueRustEnum = OpaqueRustEnum.Named(field: MyType())
    
    switch reflect_enum(opaqueRustEnum) {
        case .Named(let named):
            XCTAssertEqual(named, MyType())
            break
        default:
            XCTFail()
    }
  • Reduce the FFI representation of ZSTs (zero-sized types) from one byte to zero bytes. #178

    • Previously ZSTs were bridged as a u8 in order to preserve FFI safety.
    • Now we simply inline the ZST on both sides of the FFI boundary, enabling zero memory overhead sharing of ZSTs.
    // For example, the following bridge module:
    
    #[swift_bridge::bridge]
    mod ffi {
        struct UnitStruct1;
        struct UnitStruct2{}
        struct UnitStruct3();
    
        extern "Rust" {
            fn some_function(
                arg1: UnitStruct1,
                arg2: UnitStruct2,
                arg3: UnitStruct3,
            ) -> UnitStruct1;
        }
    }
    // generates the following exported Rust function:
    
    #[link_name = "__swift_bridge__$some_function"]
    pub extern "C" fn __swift_bridge__some_function() {
        {super::some_function(UnitStruct1, UnitStruct2{}, UnitStruct3()); ()}
    }
    // and generates the following Swift:
    
    func some_function(arg1: UnitStruct1, arg2: UnitStruct2, arg3: UnitStruct3) -> UnitStruct1 {
        { let _ = __swift_bridge__$some_function(); UnitStruct1() }()
    }