5 ms·
I'm an extreme outlier, but I'm about to start an Objective-C codebase (a 2D renderer on top of Metal), because I suspect interop with Rust will be a lot easier
by raphlinus 7y ago
I'm an extreme outlier, but I'm about to start an Objective-C codebase (a 2D renderer on top of Metal), because I suspect interop with Rust will be a lot easier. In particular, it looks like you can just build .m files from a build.rs, and I'm pretty sure that's not true with Swift. It's also possible to invoke Obj-C from Rust using the dynamic features, but I think that's both code bloat and a runtime cost. [I'm interested in exploring these issues further, but it's probably outside the scope of this HN thread, get in touch if you want to discuss.]
Example build.rs: https://github.com/emoon/rust_minifb/blob/master/build.rs https://github.com/emoon/rust_minifb/blob/master/build.rs
- saagarjha 7y agoWhy can’t you build Swift from your build.rs?
- raphlinus 7y agoMaybe I can, I just haven't found an example of it. Certainly the "cc" crate doesn't seem to know how to compile Swift files. This might be more of a documentation issue than a real technical limitation though.
- saagarjha 7y agoIf you can figure out a way to run arbitrary commands, I’d suggest looking at swift -frontend, which works like you’d expect a normal compiler to.
- suraj 7y agoIf you want to call rust from Swift, it should be easy. I work with C libraries and it easy to interface with module maps. However, with objective C the interoperability is trivial. As long as you pay attention to retain cycles, you can hold reference and call objc methods directly. That said, please make sure your framework can be imported into Swift properly. As most of the users will be using Swift.
- raphlinus 7y agoYes, I've seen examples of calling Rust from Swift, and I do plan to support that. This particular thing is about calling macOS platform functions from Rust code. Projects either call Objective-C API's from native Rust code using the dynamic invocation mechanism (which I think is slow and code-bloaty but want to measure more carefully), or they use the "cc" crate to include Objective-C code (and then generally use C ABI calls from Rust code into that). I've never seen Rust code that compiles Swift from a "cargo build" but of course would love to be proven wrong.