chinedufn / swift-bridge

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

Cannot use two `#[swift_bridge(..)]` attributes on the same function #258

Open chinedufn opened 8 months ago

chinedufn commented 8 months ago

If two #[swift_bridge(..)] attributes are used on the same funtion the first one will get ignored.

mod foo {
    extern "Rust" {
        type Foo;

        #[swift_bridge(init)]
        #[swift_bridge(associated_to = Foo)]
        fn bar () -> Foo;
    }
}

This is ignoring of the first #[swift_bridge(..)] attribute is happening here https://github.com/chinedufn/swift-bridge/blob/1b797f6595c9e45f30032db5a24403f746f5d1b0/crates/swift-bridge-ir/src/parse/parse_extern_mod.rs#L97-L102


Potential Solutions

Potential Solution 1 - Merge function attributes

First start by using .parse_args()? to parse a bunch of FunctionAttr using a new type wrapper struct RawFuncAttrs(syn::punctuated::Punctuated::<FunctionAttr, syn::Token![,]>);

Then combine them all using something like: FunctionAttributes::from_raw(attributes: &[RawFuncAttrs]).

https://github.com/chinedufn/swift-bridge/blob/1b797f6595c9e45f30032db5a24403f746f5d1b0/crates/swift-bridge-ir/src/parse/parse_extern_mod/function_attributes.rs#L67-L68

RawFuncAttrs would look similar to this https://github.com/chinedufn/swift-bridge/blob/1b797f6595c9e45f30032db5a24403f746f5d1b0/crates/swift-bridge-ir/src/parse/parse_extern_mod/function_attributes.rs#L63-L76

Potential Solution 2 - Make the user specify all attributes together

Don't allow multiple separate #[swift_bridge(..)] attributes and instead make the user say #[swift_bridge(init, associated_to = Foo)] if they want to express multiple properties.

Potential Solution 3 - ...

Trying to quickly open this issue and go do something else so I didn't think of all possible solutions.