Skip to content

Commit

Permalink
Use impl instead of ProtocolObject where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Sep 21, 2023
1 parent 6d75180 commit 392f711
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 19 deletions.
20 changes: 20 additions & 0 deletions crates/header-translator/src/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1758,6 +1758,26 @@ impl fmt::Display for Ty {
ty => write!(f, "{ty}"),
},
TyKind::MethodArgument | TyKind::FnArgument => match &self.ty {
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(())
}
Inner::Id {
ty,
is_const: false,
Expand Down
11 changes: 4 additions & 7 deletions crates/icrate/examples/browser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use objc2::{
declare_class, msg_send, msg_send_id,
mutability::MainThreadOnly,
rc::Id,
runtime::{AnyObject, ProtocolObject, Sel},
runtime::{AnyObject, Sel},
sel, ClassType,
};

Expand Down Expand Up @@ -197,12 +197,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 @@ -304,8 +302,7 @@ fn main() {
let delegate = Delegate::new(mtm);

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

// run the app
unsafe { app.run() };
Expand Down
4 changes: 1 addition & 3 deletions crates/icrate/examples/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use icrate::Foundation::{
};
use objc2::declare::{Ivar, IvarBool, IvarDrop, IvarEncode};
use objc2::rc::Id;
use objc2::runtime::ProtocolObject;
use objc2::{declare_class, msg_send, msg_send_id, mutability, ClassType};

declare_class!(
Expand Down Expand Up @@ -85,8 +84,7 @@ fn main() {
println!("{delegate:?}");

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

// run the app
unsafe { app.run() };
Expand Down
6 changes: 2 additions & 4 deletions crates/icrate/examples/metal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,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 @@ -374,8 +373,7 @@ fn main() {
let delegate = Delegate::new(mtm);

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

// run the app
unsafe { app.run() };
Expand Down
2 changes: 0 additions & 2 deletions crates/icrate/src/additions/Foundation/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSMutableDictionary<K,
.get(key)
.map(|old_obj| unsafe { util::mutable_collection_retain_removed_id(old_obj) });

let key = ProtocolObject::from_ref(key);
// SAFETY: We have ownership over the value.
unsafe { self.setObject_forKey(&value, key) };
old_obj
Expand Down Expand Up @@ -372,7 +371,6 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSMutableDictionary<K,
.get(key)
.map(|old_obj| unsafe { util::mutable_collection_retain_removed_id(old_obj) });

let key = ProtocolObject::from_ref(key);
// SAFETY: The value is `IsRetainable`, and hence safe for the
// collection to retain.
unsafe { self.setObject_forKey(value, key) };
Expand Down
2 changes: 1 addition & 1 deletion crates/icrate/src/generated
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,5 @@ fn main() {
let app = NSApplication::sharedApplication(mtm);

let delegate = CustomObject::new(mtm);
let delegate = ProtocolObject::from_ref(&*delegate);
app.setDelegate(Some(delegate));
app.setDelegate(Some(&delegate));
}

0 comments on commit 392f711

Please sign in to comment.