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 Nov 25, 2024
1 parent 63d42bf commit 3449797
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 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 @@ -1635,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

0 comments on commit 3449797

Please sign in to comment.