DEV Community

Cover image for How we built a Swift app that uses Rust

How we built a Swift app that uses Rust

Mrinal Wadhwa on December 28, 2023

Yesterday our team released Portals, an open source Mac app built in Swift. It uses the Ockam Rust library to privately share TCP or HTTP services ...
Collapse
 
drumm profile image
Sam J.

Thanks for the article!!

Just to mention that this link is not working because of the braces 😉: dev.to/build-trust/(https://github...

Collapse
 
mrinal profile image
Mrinal Wadhwa

Thank you so much for pointing that out. Just fixed it!

Collapse
 
drumm profile image
Sam J.

It’s great to see such an app being established with one of the 2 best programming languages available (some would say 3), unfortunate in 2023 we still have to rely on C for interoperability, imagine this would become Rust! 🤯

Collapse
 
cemcneill profile image
CEMcNEill

I know you guys have been working on this quietly for a while now @mrinal. So exciting to see it finally released.

Collapse
 
mrinal profile image
Mrinal Wadhwa

Thank you @cemcneill, so glad you like it!

Collapse
 
proteusiq profile image
Prayson Wilfred Daniel

Mind blowing 🤯! Thank you for documenting your journey.

Collapse
 
mrinal profile image
Mrinal Wadhwa

Thank you @proteusiq. So happy to hear that you liked it!

Collapse
 
mattgreg profile image
Matthew Gregory

Portals is a cool app. @mrinal can you elaborate on the differences between Portals and ngrok?

Collapse
 
mrinal profile image
Mrinal Wadhwa • Edited

@mattgreg Great question. Here's the key difference in behavior:

When you use ngrok your local service is exposed to the Internet which mean attackers on the Internet can try different ways to attack it and break into your computer. ngrok servers also terminate TLS and see your unencrypted application data.

When you use Portals, your service is not available on the Internet, it is ONLY exposed to your invited friends. Attackers from the Internet have no way of attacking it. Ockam servers cannot see or manipulate your application's data since it is end-to-end encrypted between your friend's computer and your computer.

Collapse
 
etorreborre profile image
Eric Torreborre

Could you say a bit more about the memory management side since Swift is garbage collected and Rust is not? At the end of the day how is memory reclaimed?

Collapse
 
davidebaldo profile image
Davide Baldo

Sure!
First, during swift toward rust interactions, we need to manually consider the scope of the validity of parameters: they will be valid only until the functions returns. Adding the async context on the rust side, we need to copy the parameters before returning the function to make sure we don't incur in any memory violation or use-after-free.
When it's rust calling swift instead, we make a similar consideration: parameters will be valid only until the function returns, subsequently, the allocated memory of structures needs to manually be freed.

Collapse
 
dev99 profile image
kalyan swaroop

What if you had used protobuf to generate the structures on the two sides ?
Would that have reduced some of your structure translation work (and made it safer perhaps)?

Collapse
 
mrinal profile image
Mrinal Wadhwa

Hi @dev99 good question. I don't think protobuf is helpful here since there's no need to serialize the data. We can simply access them in-memory.

Collapse
 
matthiasg profile image
matthiasg

So you used Tauri v2 and found it not good ? v2 is getting close to beta now, so do you think they will get better or is it of fundamental nature ?