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

not sure my use case would work #271

Open
extrawurst opened this issue Apr 26, 2024 · 4 comments
Open

not sure my use case would work #271

extrawurst opened this issue Apr 26, 2024 · 4 comments

Comments

@extrawurst
Copy link

I am looking into using swift-bridge for building native iOS bevy integrations. aka reusable crates/swift-packages.

So far I did this by hand crafting the rust-side and swift-side to then have a matching c-ffi layer in between. once this was complex enough I used protobuf for the data-transport between both sides. works but adds protobuf as a dependency and obviously requires data serialization (albeit it being fast)

now I am wondering if I can use swift-bridge instead. so far I cannot get it to work, as it seem to heavily depend on bridging headers to c headers. and as far as I can see there is no way to create swift packages shipping swift code and c headers.

maybe I am missing something? (I hope so, using this would be so much more awesome)

@chinedufn
Copy link
Owner

chinedufn commented Apr 26, 2024

If I'm understanding, you're looking to create a Swift package that contains Swift and Rust code?

Does the Swift packages chapter in the book meet your needs https://github.com/chinedufn/swift-bridge/tree/c3c950c90867550e09a24c94357f1e502bf6d69d/book/src/building/swift-packages


If that's not what you mean then could you explain your use case a bit more?

What is the final artifact that you are looking to create? How would that artifact be used?

@extrawurst
Copy link
Author

escentially i am building another project like this: https://github.com/rustunit/bevy_ios_notifications

At the root of the repo its a swift package that people can easily integrate into their xcode project via the SPM.

This package so far exposed a manually crafted c-ffi via funcs like:

@_cdecl("swift_notifications_badge_set")
public func swiftNotificationsBadgeSet(number: Int) {
    UIApplication.shared.applicationIconBadgeNumber = number
}

this is matched by a separate (though in the same git repo) rust crate that hooks into these via the usual:

extern "C" {
    fn swift_notifications_badge_set(number: i32);
}

So the communication is rust calling swift and and in order to call the other direction there some swift functions take a function pointer that is used to callback.

This way everything works together as soon as xcode builds the project and links everything together the c functions that rust expects to be there are coming from the swift package.

Now crafting these c-ffis is obviously tedious and error prone. so ideally I can use swift-bridge to generate these for me

problem:
I fail to put what it generates into a swift package that is usable in an xcode project as it does not seem to care at all about the c headers that swift-bridge generates. it basically just does not not about anything in the header files

@Bright-Shard
Copy link
Contributor

Bright-Shard commented Apr 28, 2024

I think you need to set the bridging header setting to tell XCode to link against the headers, otherwise it will just ignore them: https://developer.apple.com/documentation/swift/importing-objective-c-into-swift#Import-Code-Within-an-App-Target

@chinedufn
Copy link
Owner

Thanks Bright-Shard.


extrawurst if that doesn't work please share the build errors that you are seeing. This will help with figuring out what the root of the problem is.

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

3 participants