RustKit
RustKit copied to clipboard
Rename duplicate argument names
Let me preface this by making it clear that I have no idea what I’m doing! Still new to Rust and I don’t understand much about Obj-C interop.
I’m trying out different ways of working with Cocoa and I came across these bindings, looking really good. I ran into some difficulties getting AppKit to work, rustc doesn’t like duplicate argument names:
Compiling rustkit v0.0.1 (https://github.com/michaelwu/RustKit?rev=fd7364800c1c31718aabaa23757f7bd20db59d3b#fd736480)
error[E0415]: identifier `name` is bound more than once in this parameter list
--> ./target/debug/build/rustkit-e42b5beac1d8b484/out/AppKit.rs:76942:9
|
76942 | name: &NSString,
| ^^^^ used as parameter more than once
Here’s the generated signature:
pub fn renameFontCollectionWithName_visibility_toName_error_(
name: &NSString,
visibility: NSFontCollectionVisibility,
name: &NSString,
error: Option<&mut &Option<Arc<NSError>>>,
) -> bool {
I changed the generator to append numbers to each duplicated argument to get around this. The code is probably a little wonky, please suggest any improvements. That said, I still can’t get AppKit to work but this is probably a separate issue. Here’s my little testcase:
extern crate rustkit;
use rustkit::AppKit::NSApplication;
use rustkit::AppKit::NSApplicationActivationPolicy::NSApplicationActivationPolicyRegular;
fn main() {
let app = NSApplication::sharedApplication();
app.setActivationPolicy_(NSApplicationActivationPolicyRegular);
app.run();
}
Now it seems to crash due to unregistered selectors, but at least the bindings work:
2019-03-03 20:12:40.888 appkit[15155:116850] *** NSForwarding: warning: selector (0x10725cd65) for message 'sharedApplication' does not match selector known to Objective C runtime (0x7fff4b587a4c)-- abort
2019-03-03 20:12:40.888 appkit[15155:116850] +[NSApplication sharedApplication]: unrecognized selector sent to class 0x7fffa58f0778
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/libcore/option.rs:345:21
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
fatal runtime error: failed to initiate panic, error 5
fish: 'cargo run' terminated by signal SIGABRT (Abort)
Could it be that the method signature is used to determine the Objective-C function call? If that is the case, renaming the arguments might mess with that and therefore fires the NSForwarding
error, which only gets fired if the underlying Object doesn't implement a specific method you try to call.
That's an excellent point, I need to check how the arguments are looked up. I haven't had time to revisit this yet, unfortunately. Thanks!
No problem. Any tips on how to get this even compiled? I do run macOS Catalina Beta 10. But adding the crate to my Cargo.toml file throws an error that the feature RK_AppKit
is not available even though it is specified in the configuration file. I even used the git repo as dependency. It then started to somehow start the building process but I get errors that it can't compile. No error trace, nothing. Just that it doesn't compile. I do have the environment variable set to point to the libclang.dylib
provided by XCode.
Sounds like there's a problem with some C/C++/Obj-C dependencies, you might not see an error from rustc since they're compiled separately. Catalina removed 32-bit support, it could be due to that. I don't have access to a Mac environment at the moment, but you might want to try compiling on Mojave and see if it's due to the recent changes in Catalina.
Might be. I need Catalina for the XCode 13 version though as I use SwiftUI extensively.
I only need one or two functions within AppKit. But I couldn't find any way to call Obj-C without any third party crate. Almost all Medium or other blog entries rely on some crate that either doesn't work anymore or doesn't compile for me :(