Skip to content
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

Use impl instead of ProtocolObject where possible #517

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions crates/header-translator/src/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1416,14 +1416,18 @@ impl Ty {
Self::GenericParam { name } => write!(f, "{name}"),
Self::AnyObject { protocols } => match &**protocols {
[] => write!(f, "AnyObject"),
[decl] => write!(f, "ProtocolObject<dyn {}>", decl.id.path()),
// TODO: Handle this better
[first, rest @ ..] => {
write!(f, "AnyObject /* {}", first.id.path())?;
for protocol in rest {
write!(f, "+ {}", protocol.id.path())?;
protocols => {
write!(f, "ProtocolObject<dyn ")?;

let mut iter = protocols.iter();
let protocol = iter.next().unwrap();
write!(f, "{}", protocol.path())?;

for protocol in iter {
write!(f, " + {}", protocol.path())?;
}
write!(f, " */")?;

write!(f, ">")?;
Ok(())
}
},
Expand Down Expand Up @@ -1631,6 +1635,26 @@ impl Ty {

pub(crate) fn fn_argument(&self) -> impl fmt::Display + '_ {
FormatterFn(move |f| match self {
Inner::Id {
ty: IdType::AnyObject { protocols },
is_const: false,
lifetime: Lifetime::Unspecified | Lifetime::Strong,
nullability,
} if self.kind == TyKind::MethodArgument && !protocols.is_empty() => {
if *nullability != Nullability::NonNull {
write!(f, "Option<")?;
}
write!(f, "&")?;
write!(f, "(impl ")?;
for protocol in protocols {
write!(f, "{} + ", protocol.path())?;
}
write!(f, "Message)")?;
if *nullability != Nullability::NonNull {
write!(f, ">")?;
}
Ok(())
}
Self::Pointer {
nullability,
is_const: _,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ extern_methods!(
}
);

fn main() {}
fn main() {
let mtm = MainThreadMarker::new().unwrap();
let app = NSApplication::sharedApplication(mtm);

let delegate = CustomObject::new(mtm);
app.setDelegate(Some(&delegate));
}
3 changes: 1 addition & 2 deletions framework-crates/objc2-app-kit/examples/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ fn main() {

// configure the application delegate
let delegate = AppDelegate::new(42, true, mtm);
let object = ProtocolObject::from_ref(&*delegate);
app.setDelegate(Some(object));
app.setDelegate(Some(&*delegate));

// run the app
app.run();
Expand Down
1 change: 0 additions & 1 deletion framework-crates/objc2-foundation/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,6 @@ impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectT
where
CopiedKey: Message + NSCopying + CopyingHelper<Result = KeyType>,
{
let key = ProtocolObject::from_ref(key);
// SAFETY: The key is copied, and then has the correct type `KeyType`.
unsafe { self.setObject_forKey(object, key) };
}
Expand Down
6 changes: 2 additions & 4 deletions framework-crates/objc2-metal/examples/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ declare_class!(

// configure the metal view delegate
unsafe {
let object = ProtocolObject::from_ref(self);
mtk_view.setDelegate(Some(object));
mtk_view.setDelegate(Some(self));
}

// configure the window
Expand Down Expand Up @@ -310,8 +309,7 @@ fn main() {

// configure the application delegate
let delegate = Delegate::new(mtm);
let object = ProtocolObject::from_ref(&*delegate);
app.setDelegate(Some(object));
app.setDelegate(Some(&*delegate));

// run the app
app.run();
Expand Down
9 changes: 3 additions & 6 deletions framework-crates/objc2-web-kit/examples/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,10 @@ declare_class!(

unsafe {
// handle input from text field (on <ENTER>, load URL from text field in web view)
let object = ProtocolObject::from_ref(self);
nav_url.setDelegate(Some(object));
nav_url.setDelegate(Some(self));

// handle nav events from web view (on finished navigating, update text area with current URL)
let object = ProtocolObject::from_ref(self);
web_view.setNavigationDelegate(Some(object));
web_view.setNavigationDelegate(Some(self));
}

// create the menu with a "quit" entry
Expand Down Expand Up @@ -311,8 +309,7 @@ fn main() {

// configure the application delegate
let delegate = Delegate::new(mtm);
let object = ProtocolObject::from_ref(&*delegate);
app.setDelegate(Some(object));
app.setDelegate(Some(&*delegate));

// run the app
app.run();
Expand Down
Loading