News for hackers. Stuff that matters.
Re:Objective-Rust by cornstalks on Friday August 28, 2020 @06:03PM (#24310629)
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.
Re:Objective-Rust by epage on Friday August 28, 2020 @08:05PM (#24311889)
This is reminding me that I'd love to see experiments with macro's for implementing Swift's ABI, not for Swift interop but to explore the space of a more native, unsafe-free SO/DLL ABI for Rust than what C offers.
Re:Objective-Rust by terhechte on Saturday August 29, 2020 @12:48AM (#24313641)
https://github.com/rustswift/swift-bindgen

tries to offer #[repr(Swift_5)]

Re:Objective-Rust by cornstalks on Friday August 28, 2020 @10:07PM (#24312674)
My plan has always been to branch out and try to support Swift once objrs is more mature. But I haven't explored the Swift ABI at all so I might only be able to realistically support a small subset of Swift, if anything at all.
Re:Objective-Rust by Filligree on Saturday August 29, 2020 @08:50AM (#24315726)
> 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.

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?

Re:Objective-Rust by viraptor on Saturday August 29, 2020 @09:03AM (#24315797)
> I hear this story a lot about Rust, and it makes me wary of the language.

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.

Re:Objective-Rust by WesolyKubeczek on Saturday August 29, 2020 @10:11AM (#24316225)
It’s the bit where it has to be a nightly but not a too new nightly that gives heebiejeebies.

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.

Re:Objective-Rust by SAI_Peregrinus on Saturday August 29, 2020 @11:56AM (#24316975)
Nightly is totally unstable. Features get added, and might get removed if the team decides they're not a good idea. So programs that want to use such an experimental feature might only work for a certain few versions of Nightly, because Nightly only guarantees the stability of features that have been released to Stable.

That's why it's strongly recommended not to use Nightly to write your programs: they'll almost certainly break, and since Nightly gets released every 24 hours they might break daily. If you don't have the time to fix things every day, you shouldn't be using the unstable features!

Re:Objective-Rust by lokedhs on Saturday August 29, 2020 @11:44AM (#24316866)
I've looked at Rust a few times, and every time I installed the stable release, but as soon as I wanted to actually do something, I realise that the library or feature I wanted to use was not actually available in stable.

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?

Re:Objective-Rust by steveklabnik on Saturday August 29, 2020 @01:08PM (#24317582)
In our 2018 survey, 56% of people used nightly. In 2019, it was down to 30%.

This question is always multiple choice, so people who use Rust a lot may choose both nightly and stable. The vast majority of my projects target stable, with my operating system ones being the exception that use nightly.

Re:Objective-Rust by cornstalks on Saturday August 29, 2020 @09:38AM (#24315988)
Rewriting it has nothing to do with it being in Rust. The reality is that I don't have a lot of free time, but I really wanted to get the project into a functioning state (even if it sucked). So I took some shortcuts a couple years ago in order to get it fully functioning (more or less) in a short amount of time. I would have taken those same shortcuts in any language.
Re:Objective-Rust by saagarjha on Friday August 28, 2020 @06:11PM (#24310746)
Cool! Does it support laying out ivars too?
Re:Objective-Rust by cornstalks on Friday August 28, 2020 @06:24PM (#24310930)
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...