[HN Gopher] Objective-Rust ___________________________________________________________________ Objective-Rust Author : ingve Score : 80 points Date : 2020-08-28 20:28 UTC (2 hours ago) (HTM) web link (belkadan.com) (TXT) w3m dump (belkadan.com) | jasonhansel wrote: | I 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 wrote: | Swift 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 wrote: | Swift selectively shares the objc memory model. | cornstalks wrote: | Funny enough, this is exactly what my objrs crate is meant for: | 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 wrote: | Cool! Does it support laying out ivars too? | cornstalks wrote: | Yeah, 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 | | [2]: https://gitlab.com/objrs/objrs/-/tree/master/demo | | [3]: https://gitlab.com/objrs/objrs/-/blob/master/frameworks/ | foun... | samatman wrote: | Seeing the headline gave me an immediate rush, mixing horror and | fascination. | | The article did not disappoint. | Trufa wrote: | Sometimes I'm truly amazed with what fellow programmers are able | to do. ___________________________________________________________________ (page generated 2020-08-28 23:00 UTC)