Brendonovich / swift-rs

Call Swift functions from Rust with ease!
Apache License 2.0
253 stars 30 forks source link

what a some scenarios where such interoperability could be useful? #1

Closed shengchl closed 2 years ago

shengchl commented 2 years ago

Hey, just been wandering here and there when i landed on this project. This questions is out of pure curiosity as I am mostly a jr. programmer but rust<->swift interoperability looks like kind of exotic thing. I see that for both rust and swift its important to have access to the legacy of c world, but what are I some potential real world scenarios where swift-rust interop is a thing?

Just to make things clear - I am genuinely asking and appreciate the work done on this project!

Brendonovich commented 2 years ago

Hey, thanks for asking! To your point I haven't documented why you may want to use swift-rs, but I can tell you why I created it initially, which should answer your question.

I was helping my friend Jamie Pine with one of projects, which is a desktop app made with Tauri, an Electron alternative that uses Rust as its backend. One of the features Jamie wanted was to get the preview icon for files on his filesystem, which can be done with the icon(forFile:) function on the app's NSWorkspace. This requires accessing the static shared property of NSWorkspace, something that after some research wasn't possible using the Rust Objective-C bindings (since from what I can tell it only supports sending and receiving messages, not accessing static properties), and I could figure out if swift-bindgen could do the job. So I created this library and the rest is history!

The examples folder is actually the same Swift code that Jamie uses in his project. While there's probably other, less unsafe ways to interop with Swift, its been both my and Jamie's experience that leveraging Swift for it's native API access and Rust for building applications is quite nice compared to wrangling Swift with calls from Rust similar to how the objc crate has you do. This library probably has a littany of problems around memory management and leaks since I'm not that well versed in the Swift runtime, but it gets the job done where needed!

Brendonovich commented 2 years ago

@shengchalover I'm going to close this issue but feel free to keep commenting if you have any further questions

shengchl commented 2 years ago

Thank you very much for detailed response. This is indeed a very interesting example of language interoperability!

tleyden commented 7 months ago

@Brendonovich

This library probably has a littany of problems around memory management and leaks

This comment was written a few years ago, just wondering if swift-rs is considered stable at this point?

Brendonovich commented 7 months ago

@tleyden Stable enough that Spacedrive hasn't experienced any problems and Tauri are using it for their iOS support. There's been some pretty big changes since that comment was written, and the way swift-rs manages memory is predictable enough to where I'd be confident recommending it to other people.