6 ms·
Objective-Rust
- samatman 6y agoSeeing the headline gave me an immediate rush, mixing horror and fascination. The article did not disappoint.
- Trufa 6y agoSometimes I’m truly amazed with what fellow programmers are able to do.
- jasonhansel 6y agoI actually think this could be developed into something very useful. Rust is a lot cleaner than Swift, and proper Objective-C interop could make it a viable alternative for app development. (I'd avoid trying to replicate ObjC's syntax, though, since it's the worst part of that language.)
- saagarjha 6y agoSwift and Objective-C share a memory model, though, which Rust does not. I suspect that any Rust code that interoperates with Objective-C will never be "cleaner" than the corresponding Swift code.
- adamnemecek 6y agoSwift selectively shares the objc memory model.
- Klonoar 6y agoI mean, it depends on the Swift code you write - basic example which could be done fairly closely: https://github.com/ryanmcgrath/cacao/blob/trunk/examples/autolayout.rs https://github.com/ryanmcgrath/cacao/blob/trunk/examples/aut... 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.
- fiddlerwoaroof 6y agoI’ve had some pretty good luck integrating Common Lisp and the Objective-C runtime: the difference of memory models isn’t a huge problem, as long as there’s a nice way to map between them.
- smabie 6y agoApps don't need to be in Rust. And maybe they even shouldn't be programmed in Rust. 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.
- zozbot234 6y agoSwift does not do cycle-collecting GC, either. Not really any "higher-level" than Rust, when you look at a comprehensive view of the problem space. And obligate ARC ala Swift has all kinds of performance pitfalls, to the point where it can be even worse than obligate tracing GC.
- smabie 6y agoARC is GC, and anyone who says otherwise is crazy.
- saagarjha 6y agoUsually on Apple’s platforms ARC and GC are treated separately because Objective-C has shipped with a conventional garbage collector as well as reference counting and it’s difficult to tell them apart otherwise.
- jmull 6y agoYes. It might help to can think of it this way: 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.
- cornstalks 6y agoFunny enough, this is exactly what my objrs crate is meant for: https://gitlab.com/objrs/objrs https://gitlab.com/objrs/objrs 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.
- saagarjha 6y agoCool! Does it support laying out ivars too?
- cornstalks 6y agoYeah, it supports ivars just fine. Computing byte offsets for each ivar was a pain the butt. By default it will use the field's name as the ivar name. You can rename the ivar by using #[objrs(ivar, name = "whatever")] on the ivar. You can also pass an expression to be used to construct the ivar and give it a default value. 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 https://gitlab.com/objrs/objrs/blob/master/DOCUMENTATION.md [2]: https://gitlab.com/objrs/objrs/-/tree/master/demo https://gitlab.com/objrs/objrs/-/tree/master/demo [3]: https://gitlab.com/objrs/objrs/-/blob/master/frameworks/foundation/src/nsobject.rs https://gitlab.com/objrs/objrs/-/blob/master/frameworks/foun...
- xixixao 6y agoI tried something similar in Nim. While fun, ultimately I was faced with the reality of a lack of proper typing and other tooling support, which would be a mountain of work to add on top.
- raphlinus 6y agoI hope Rust / Objective-C interop gets some more love soon. There are a number of pain points, including inconsistent wrapping styles (the "cocoa" crate implements things like NSView as traits on id), autorelease pools always being a pain, and a fair amount of bloat from the msg_send! macro (a lot of which is boilerplate error handling). 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.
- Klonoar 6y agoI experimented with this in cacao: https://github.com/ryanmcgrath/cacao https://github.com/ryanmcgrath/cacao 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.
- raphlinus 6y agoYup, that is a promising direction. I think a lot of my concern is around building a consensus and getting people on the same page.
- JacobCarlborg 6y agoD has had native interoperability support for Objective-C for a couple of years now: https://dlang.org/spec/objc_interface.html https://dlang.org/spec/objc_interface.html.