You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a bummer because you need to repeat the types that you've already specified.
This wasn't always the case for Rust, but this behavior changed in rust-lang/rust#19891. rust-lang/rust#21086 was opened about this, but was closed as expected behavior.
Unfortunately not, the compiler doesn't seem to be able to tell what we mean by that cast. Gives an error like:
error[E0277]: the trait bound `extern "C" fn(_): declare::MethodImplementation` is not satisfied
--> src/test_utils.rs:104:18
|
104 | decl.add_method(sel!(setFoo:), custom_obj_set_foo as extern fn(_));
| ^^^^^^^^^^ the trait `declare::MethodImplementation` is not implemented for `extern "C" fn(_)`
@quadrupleslap ... huh, that's very weird. I was able to repro with this error message:
error[E0277]: the trait bound `for<'r, 's> extern "C" fn(&'r mut objc::runtime::Object, objc::runtime::Sel, &'s mut objc::runtime::Object): objc::declare::MethodImplementation` is not satisfied
the trait `objc::declare::MethodImplementation` is not implemented for `for<'r, 's> extern "C" fn(&'r mut objc::runtime::Object, objc::runtime::Sel, &'s mut objc::runtime::Object)`
help: the following implementations were found:
<for<'r> extern "C" fn(&'r mut T, objc::runtime::Sel, A) -> R as objc::declare::MethodImplementation>
<for<'r> extern "C" fn(&'r T, objc::runtime::Sel, A) -> R as objc::declare::MethodImplementation>
I thought maybe that the implementation of MethodImplementation was inferring some 'static bounds, but adding generic lifetime bounds to the argument types didn't help. I've tried contorting the declaration various ways and adding all sorts of zany lifetime bounds and for<'a>, but I'm stumped.
I'm actually having a tough time with this. Very new to ObjC, just trying to knock together a Rust binding for a small MacOS API surface I need for my crate so I can avoid ObjC entirely.
I'm trying to implement a callback for this delegate and stay as close to ObjC as I can (I.e. minimal use of cocoa and other crates, which don't implement this API anyway.) I have:
error[E0277]: the trait bound `for<'r, 's> extern "C" fn(&'r objc::runtime::Object, objc::runtime::Sel, &'s objc::runtime::Object, i8): objc::declare::MethodImplementation` is not satisfied
--> src/backends/ns_speech_synthesizer.rs:31:17
|
31 | / speech_synthesizer_did_finish_speaking
32 | | as extern "C" fn(&Object, Sel, &Object, BOOL) -> (),
| |_______________________________________________________________________^ the trait `objc::declare::MethodImplementation` is not implemented for `for<'r, 's> extern "C" fn(&'r objc::runtime::Object, objc::runtime::Sel, &'s objc::runt
ime::Object, i8)`
|
= help: the following implementations were found:
<for<'r> extern "C" fn(&'r T, objc::runtime::Sel, A, B) -> R as objc::declare::MethodImplementation>
<for<'r> extern "C" fn(&'r mut T, objc::runtime::Sel, A, B) -> R as objc::declare::MethodImplementation>
I've looked at the definition of MethodImplementation but it appears to be created by macros, and mentally parsing macros isn't my strong suit. Removing the third argument (I.e. second &Object arg) makes this compile, but this forum post seemed to indicate that was the wrong method declaration for the delegate I'm trying to implement. The delegate is never called, even though I manually create and run a NSRunLoop in a console-based example, so I'm strongly suspecting that I'm not defining my method correctly for the delector the delegate is checking for.
Anyway, not sure where else to turn for help with this, but having spent 6 hours trying to figure out how to match this delegate selector to a method and failing does suggest that method definition could be a bit more ergonomic. :) Otherwise, thanks for creating this. Didn't take me very long to call into MacOS ObjC APIs without knowing a whole lot about them, and I'm hopeful that this delegate issue is the last I'll face with ObjC interop for this particular task (though maybe that's a bit naive of me. :)
So the issue is... Quite complex, and honestly feels more like a bug in the compiler (though I'm not really qualified to state that).
In short, it's because our implementation of MethodImplementation for fn-pointers doesn't (can't with current compiler as far as I know) take into account higher-rank trait bounds, but when coercing your function to a function pointer, you're implicitly telling the compiler that the arguments have higher-ranked lifetimes.
This won't work because methods have more than one argument, but you're only specifying one with extern fn(_).
But even if you specified the correct number of arguments (extern fn(_, _, _)), it wouldn't work either because our implementation of MethodImplementation requires the first parameter to have a higher-ranked lifetime.
modified_fun asfor<'a> extern"C"fn(&'amutObject,Sel,&'staticmutObject)
fun asfor<'a, b> extern"C"fn(&'amutObject,Sel,&'bmutObject)
And only the first one matches our implementation of MethodImplementation, &'static mut Object can be substituted for A: Encode, but for<'b> &'b mut Object isn't a real type, and as such can't.
Activity
[-]Creating a MethodDecl isn't ergonomic[/-][+]Adding a method isn't ergonomic[/+]aldanor commentedon Mar 21, 2017
Would this work?
SSheldon commentedon May 29, 2017
Unfortunately not, the compiler doesn't seem to be able to tell what we mean by that cast. Gives an error like:
quadrupleslap commentedon Jan 30, 2018
This is probably its own issue, but why does this implement Encoding...
... when this doesn't?
SSheldon commentedon Feb 4, 2018
@quadrupleslap ... huh, that's very weird. I was able to repro with this error message:
I thought maybe that the implementation of
MethodImplementationwas inferring some'staticbounds, but adding generic lifetime bounds to the argument types didn't help. I've tried contorting the declaration various ways and adding all sorts of zany lifetime bounds andfor<'a>, but I'm stumped.ndarilek commentedon Aug 12, 2020
I'm actually having a tough time with this. Very new to ObjC, just trying to knock together a Rust binding for a small MacOS API surface I need for my crate so I can avoid ObjC entirely.
I'm trying to implement a callback for this delegate and stay as close to ObjC as I can (I.e. minimal use of cocoa and other crates, which don't implement this API anyway.) I have:
That gives me:
I've looked at the definition of
MethodImplementationbut it appears to be created by macros, and mentally parsing macros isn't my strong suit. Removing the third argument (I.e. second&Objectarg) makes this compile, but this forum post seemed to indicate that was the wrong method declaration for the delegate I'm trying to implement. The delegate is never called, even though I manually create and run aNSRunLoopin a console-based example, so I'm strongly suspecting that I'm not defining my method correctly for the delector the delegate is checking for.Anyway, not sure where else to turn for help with this, but having spent 6 hours trying to figure out how to match this delegate selector to a method and failing does suggest that method definition could be a bit more ergonomic. :) Otherwise, thanks for creating this. Didn't take me very long to call into MacOS ObjC APIs without knowing a whole lot about them, and I'm hopeful that this delegate issue is the last I'll face with ObjC interop for this particular task (though maybe that's a bit naive of me. :)
ndarilek commentedon Aug 12, 2020
I cracked it with some help. The second
&Objectneeds to be*const Object. With that change, plus my use ofNSRunLoop, delegates now seem to work.Merge pull request SSheldon#12 from madsmtm/pointer-width-16
madsmtm commentedon May 28, 2022
So the issue is... Quite complex, and honestly feels more like a bug in the compiler (though I'm not really qualified to state that).
In short, it's because our implementation of
MethodImplementationforfn-pointers doesn't (can't with current compiler as far as I know) take into account higher-rank trait bounds, but when coercing your function to a function pointer, you're implicitly telling the compiler that the arguments have higher-ranked lifetimes.See this playground link, and the Rust tracking issue rust-lang/rust#56105.
To make this concrete, I'll try to answer some of the questions that has been raised here:
This won't work because methods have more than one argument, but you're only specifying one with
extern fn(_).But even if you specified the correct number of arguments (
extern fn(_, _, _)), it wouldn't work either because our implementation ofMethodImplementationrequires the first parameter to have a higher-ranked lifetime.These two snippets desugar to:
And only the first one matches our implementation of
MethodImplementation,&'static mut Objectcan be substituted forA: Encode, butfor<'b> &'b mut Objectisn't a real type, and as such can't.You can fix this by doing:
But your solution with
*const Objectalso works.Consistently implement `MethodImplementation` wrt. lifetimes