RustKit icon indicating copy to clipboard operation
RustKit copied to clipboard

Rename duplicate argument names

Open lnikkila opened this issue 5 years ago • 5 comments

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)

lnikkila avatar Mar 03 '19 18:03 lnikkila

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.

mainrs avatar Oct 08 '19 14:10 mainrs

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!

lnikkila avatar Oct 08 '19 14:10 lnikkila

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.

mainrs avatar Oct 08 '19 15:10 mainrs

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.

lnikkila avatar Oct 08 '19 19:10 lnikkila

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 :(

mainrs avatar Oct 08 '19 21:10 mainrs