206 points · by ingve · 6y ago · 33 comments
You can implement new Objective-C classes directly in Rust. Or use Objective-C classes/methods from Rust. objrs is mostly a collection of macros that transforms the Rust code to be ABI-compatible with normal Objective-C code. There isn't any runtime work done by objrs. It's all done at compile time.
Here's an example of creating an new class:
#[objrs(class, super = NSObject)]
pub struct StringCollection {
strings: Vec<String>,
}
#[objrs(impl)]
impl StringCollection {
#[objrs(selector = "new", no_impl)]
pub fn new() -> objrs::Strong<StringCollection> {}
#[objrs(selector = "printStrings")]
pub fn print_strings(&self) {
for string in self.strings.iter() {
println!("String: {}", string);
}
}
}
I'm still slowly rewriting it. It requires nightly Rust, and unfortunately doesn't compile with the latest nightly. And it doesn't currently build from HEAD. But it's been a fun hobby project.tries to offer #[repr(Swift_5)]
I hear this story a lot about Rust, and it makes me wary of the language. Code that's written needs to keep working. What made you decide to use nightly in the first place?
Why wary? The whole point of experimental nightly is that you can try out new exciting features which are not stabilised yet. Sometimes they will be changed before a full release and you have to deal with it.
But if you stay with stable releases, code keeps working.
Kind of reminds of some godawful government websites which had very finicky requirements in which their ActiveX components would run: precise Internet Explorer builds, with these updates but NOT WITH THESE updates, and god forbid you update your Windows XP past SP1.
At least at the time (a year or so ago perhaps) it was not possible to actually use stable for regular development. Has this changed now?
Documentation for the various macros is at [1], which includes a number of small examples. There's a larger demo[2] that uses Metal to render an interactive image. There are also framework bindings for some common frameworks (I want to add bindings for all of Apple's frameworks, but I need finish the rewrite first). Those bindings include some uses of ivars on extern classes too[3].
[1]: https://gitlab.com/objrs/objrs/blob/master/DOCUMENTATION.md
[2]: https://gitlab.com/objrs/objrs/-/tree/master/demo
[3]: https://gitlab.com/objrs/objrs/-/blob/master/frameworks/foun...
Not exactly obj-C, but there are also problems two inconsistent ways of wrapping other macOS types: some (like the Metal crate) use foreign_types, while others use the TCFType mechanism. Among other things, these have two different meanings for "Ref" types, which can be very confusing.
So it's possible to get stuff done, but it doesn't feel nice. By contrast, the Rust/Windows world pretty well organized, with Peter Atashian's winapi providing a solid foundation, the wio crate being a good way to get ComPtr and other utilities, and Microsoft's com-rs coming soon as a standard way to do Rust-side implementation of COM interfaces.
I just need to get around to "finishing" it. I think most of the other approaches miss what people would realistically need out of ObjC/Rust interop, so... well, that's my hat in the ring.
The article did not disappoint.
(I'd avoid trying to replicate ObjC's syntax, though, since it's the worst part of that language.)
Having written my fair share of both languages, I don't think they're as far apart as people think... yet, anyway.
I doubt my response will age well.
If your application or backend or whatever can tolerate GC, I would strongly recommend that you use it. While Rust has made manual memory management easier than either C++ or C, it still requires more work than a GC'd language.
Rust has a lot of things going for it, but this obsession with writing everything in Rust that has taken HN by storm is a little confusing.
Swift is unequivocally a higher-level language than Rust and definitely should be used for iOS development unless you have a very good reason to use Rust.
Rust, with the borrow-checker, strong-arms the developer into using memory patterns that the compiler manage handle without the overhead of reference counting or other runtime mechanism.
Those same memory patterns are available in Swift as well, which the compiler can then optimize as well (in theory -- as far as I'm aware there are various unimplemented optimization opportunities in Swift).
So in a sense, in Swift you can opt-in or out of rust-level memory management.
Of course, a big difference is that Rust tells you up-front when you've got it right, but in Swift it's not at all clear. You need to profile. Swift does have a clear delineation between reference types (which use ARC) and values types, so you can get on the right path by choosing value types. Value types in Swift are very rich and powerful so it's not a hard path to follow.
Swift is much higher-level than Rust. It's at best at Go's level. You'd expect Swift's performance to be good, but in low-level tasks Swift is actually closer to JS/TypeScript than anything else: https://github.com/ixy-languages/ixy-languages