5 ms·
Funny 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
by cornstalks 6y ago
Funny 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...
- epage 6y agoThis 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.
- cornstalks 6y agoMy 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.
- terhechte 6y agohttps://github.com/rustswift/swift-bindgen https://github.com/rustswift/swift-bindgen tries to offer #[repr(Swift_5)]
- Filligree 6y ago> 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?
- viraptor 6y ago> 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.
- WesolyKubeczek 6y agoIt’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.
- SAI_Peregrinus 6y agoNightly 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!
- lokedhs 6y agoI'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?