-
Notifications
You must be signed in to change notification settings - Fork 743
[Objc] Add From<ChildClass>, TryFrom<ParentClass>, function ownership updates and Protocol inheritance #1883
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
Merged
emilio
merged 11 commits into
rust-lang:master
from
simlay:objc-into-parent-objects-and-borrow
Sep 16, 2020
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b1b1d03
Initial stuff for changing ownership and adding inheritance
simlay 64a16c9
Updates base on comments
simlay 600cbdd
ran cargo fmt
simlay c50ca59
Merge branch 'master' of github.com:rust-lang/rust-bindgen into objc-…
simlay d9d030d
First attempt to fix CI
simlay 314cca5
Fix spacing issue
simlay 0054fab
Updates from PR comments
simlay e4fb4a7
Updated CHANGELOG.md
simlay 6c5f592
Updates for CI
simlay 5d5c5a5
Update to fix CI
simlay b88fc1e
Fix test for CI
simlay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3806,7 +3806,7 @@ fn objc_method_codegen( | |
} | ||
} else { | ||
let fn_args = fn_args.clone(); | ||
let args = iter::once(quote! { self }).chain(fn_args.into_iter()); | ||
let args = iter::once(quote! { &self }).chain(fn_args.into_iter()); | ||
quote! { | ||
( #( #args ),* ) #fn_ret | ||
} | ||
|
@@ -3825,7 +3825,7 @@ fn objc_method_codegen( | |
} | ||
} else { | ||
quote! { | ||
msg_send!(self, #methods_and_args) | ||
msg_send!(*self, #methods_and_args) | ||
} | ||
}; | ||
|
||
|
@@ -3901,7 +3901,7 @@ impl CodeGenerator for ObjCInterface { | |
if !self.is_category() && !self.is_protocol() { | ||
let struct_block = quote! { | ||
#[repr(transparent)] | ||
#[derive(Clone, Copy)] | ||
#[derive(Clone)] | ||
pub struct #class_name(pub id); | ||
impl std::ops::Deref for #class_name { | ||
type Target = objc::runtime::Object; | ||
|
@@ -3921,7 +3921,9 @@ impl CodeGenerator for ObjCInterface { | |
} | ||
}; | ||
result.push(struct_block); | ||
let mut protocol_set: HashSet<ItemId> = Default::default(); | ||
for protocol_id in self.conforms_to.iter() { | ||
protocol_set.insert(*protocol_id); | ||
let protocol_name = ctx.rust_ident( | ||
ctx.resolve_type(protocol_id.expect_type_id(ctx)) | ||
.name() | ||
|
@@ -3962,6 +3964,53 @@ impl CodeGenerator for ObjCInterface { | |
} | ||
}; | ||
result.push(impl_trait); | ||
for protocol_id in parent.conforms_to.iter() { | ||
if protocol_set.insert(*protocol_id) { | ||
let protocol_name = ctx.rust_ident( | ||
ctx.resolve_type( | ||
protocol_id.expect_type_id(ctx), | ||
) | ||
.name() | ||
.unwrap(), | ||
); | ||
let impl_trait = quote! { | ||
impl #protocol_name for #class_name { } | ||
}; | ||
result.push(impl_trait); | ||
} | ||
} | ||
if !parent.is_template() { | ||
let parent_struct_name = parent.name(); | ||
let child_struct_name = self.name(); | ||
let parent_struct = ctx.rust_ident(parent_struct_name); | ||
let from_block = quote! { | ||
impl From<#class_name> for #parent_struct { | ||
fn from(child: #class_name) -> #parent_struct { | ||
#parent_struct(child.0) | ||
} | ||
} | ||
}; | ||
result.push(from_block); | ||
|
||
let error_msg = format!( | ||
"This {} cannot be downcasted to {}", | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. I think this can't use |
||
type Error = &'static str; | ||
fn try_from(parent: #parent_struct) -> Result<#class_name, Self::Error> { | ||
let is_kind_of : bool = unsafe { msg_send!(parent, isKindOfClass:class!(#class_name))}; | ||
if is_kind_of { | ||
Ok(#class_name(parent.0)) | ||
} else { | ||
Err(#error_msg) | ||
} | ||
} | ||
} | ||
}; | ||
result.push(try_into_block); | ||
} | ||
parent.parent_class | ||
} else { | ||
None | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.