-
Notifications
You must be signed in to change notification settings - Fork 698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Objc] Add From<ChildClass>, TryFrom<ParentClass>, function ownership updates and Protocol inheritance #1883
[Objc] Add From<ChildClass>, TryFrom<ParentClass>, function ownership updates and Protocol inheritance #1883
Conversation
For the ref in self, the change seems sane, but I don't have any strong feelings about it. Is there any visible change in user code except that there is no Copy bound, which I guess would encourage to put clones/retains in the right places? The From trait I'm less sure about. I guess I would prefer maybe a method something like
where the body would do a |
Yeah. There's a small chance of needing to put a
For the |
Oh yeah, now I get it. These changes seem fine to me! |
* Added TryInto trait implementation from parent to child interfaces. * Added HashSet for protocols so that the protocol inheritance works as well.
…into-parent-objects-and-borrow
I went ahead and added generation for I've gone ahead and tested this out with: #[test]
fn uiview_downcasting_tests() {
use uikit_sys::{
UIButton,
INSObject,
UIView,
UILabel,
};
use std::convert::TryFrom;
let button = UIButton(unsafe {UIButton::alloc().init()});
let as_view : UIView = button.into();
let as_button = UIButton::try_from(as_view.clone());
assert!(as_button.is_ok());
let as_uilabel = UILabel::try_from(as_view);
assert!(as_uilabel.is_err());
println!("AS UILABEL: {:?}", as_uilabel.err())
} And it worked as intended. Also, I recently noticed that the |
@emilio would you like me to add entries in the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you can, writing changelog entries would be great and save me some time when doing releases, though not huge deal, your call :)
111b312
to
e4fb4a7
Compare
@emilio Is there anything else I can do to get this merged? |
It's unclear to me what the deal is with travis though. One of the two builds finished but the other is "queued". |
No, sorry, I just need to get to reviewing it again. Really sorry for the lag :( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, just one minor change, but I see that's already an issue with Deref
implementations (and I guess it doesn't make a lot of sense to use objc
with no-std...)
So perhaps not a big deal.
@@ -3962,6 +3964,53 @@ impl CodeGenerator for ObjCInterface { | |||
} | |||
}; | |||
result.push(impl_trait); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is getting kinda gnarly, it may be worth factoring this if let ... { .. } else { .. }
to its own function. Follow-up is fine.
parent_struct_name, child_struct_name | ||
); | ||
let try_into_block = quote! { | ||
impl std::convert::TryFrom<#parent_struct> for #class_name { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can't use std
if the no_std
flag is set, so this should use trait_prefix
. Also, probably should use the old style root paths (::std::convert::TryFrom
) for consistency with other code and to support Rust 2015.
First off, I don't wanna spend forever trying to build the perfect objective-c bindings because I think that might never end. I think this PR adds a bit to the ergonomics to the generated bindings.
In this PR, we add
impl From<Bar> for Foo
whereBar
is a subclass ofFoo
in the objective-c. This will be done for all super classes ofBaz
. The reason why this will be nice is that whatever uses these bindings will want to deal with the generics of stuff. In my case, I plan to create UIKit views using specific types (UILabel
,UIButton
,UITextInput
, etc.) and then turn them into aUIView
. If there's strong push back against this feature, I suppose using aBox<dyn UIView>
will have to work.unsafe fn methodWithFoo_(self, foo: Foo)
tounsafe fn methodWithFoo_(&self, foo: Foo)
and removing theCopy
as a derived trait. One aspect of this that I've got mixed feelings about is that the existence of&self
implies that there should be a&mut self
and that's just not something that's all that feasible across the board. Withgetters/setters
it's probably possible to have&self
and&mut self
respectively. With things likeaddSubview_
, it modifies the instance of theUIView
.I'm also temped to try and move to
unsafe fn methodWithFoo_(&self, foo: &Foo)
but I'm less interested in that is because those things are commonly actually taking ownership. For example,root_view.setBackgroundColor_(UIColor::redColor())
. You don't really need the color after this (though you may need to deallocate it). But as a counter pointroot_view.addSubview_(my_button)
, you want to keep track ofmy_button
because you're going to need to run[removeFromSuperview_
](https://developer.apple.com/documentation/uikit/uiview/1622421-removefromsuperview?language=objc) and then free the memory (or not depending on your use case).@scoopr I think you might have some opinions on this.