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

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

Open
chinedufn opened this issue Mar 14, 2024 · 0 comments
Open

Comments

@chinedufn
Copy link
Owner

chinedufn commented Mar 14, 2024

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

ForeignItem::Fn(func) => {
let mut attributes = FunctionAttributes::default();
for attr in func.attrs.iter() {
attributes = attr.parse_args()?;
}


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]).

let punctuated =
syn::punctuated::Punctuated::<FunctionAttr, syn::Token![,]>::parse_terminated(input)?;

RawFuncAttrs would look similar to this

impl Parse for FunctionAttributes {
fn parse(input: ParseStream) -> syn::Result<Self> {
let mut attributes = FunctionAttributes::default();
let punctuated =
syn::punctuated::Punctuated::<FunctionAttr, syn::Token![,]>::parse_terminated(input)?;
for attr in punctuated.into_iter() {
attributes.store_attrib(attr);
}
Ok(attributes)
}
}

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.

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

No branches or pull requests

1 participant