From 3449797cdccb096ec84ed25cf2a08717c7445d11 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Mon, 11 Sep 2023 17:23:02 +0200 Subject: [PATCH] Use `impl` instead of ProtocolObject where possible --- crates/header-translator/src/rust_type.rs | 20 +++++++++++++++++++ ...clare_class_delegate_not_mainthreadonly.rs | 8 +++++++- .../objc2-app-kit/examples/delegate.rs | 3 +-- .../objc2-foundation/src/dictionary.rs | 1 - .../objc2-metal/examples/triangle.rs | 6 ++---- .../objc2-web-kit/examples/browser.rs | 9 +++------ 6 files changed, 33 insertions(+), 14 deletions(-) diff --git a/crates/header-translator/src/rust_type.rs b/crates/header-translator/src/rust_type.rs index c953687b9..6f02776de 100644 --- a/crates/header-translator/src/rust_type.rs +++ b/crates/header-translator/src/rust_type.rs @@ -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: _, diff --git a/crates/test-ui/ui/declare_class_delegate_not_mainthreadonly.rs b/crates/test-ui/ui/declare_class_delegate_not_mainthreadonly.rs index 0707fe667..ae46f3616 100644 --- a/crates/test-ui/ui/declare_class_delegate_not_mainthreadonly.rs +++ b/crates/test-ui/ui/declare_class_delegate_not_mainthreadonly.rs @@ -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)); +} diff --git a/framework-crates/objc2-app-kit/examples/delegate.rs b/framework-crates/objc2-app-kit/examples/delegate.rs index 63b2ef1a6..e44d96f2b 100644 --- a/framework-crates/objc2-app-kit/examples/delegate.rs +++ b/framework-crates/objc2-app-kit/examples/delegate.rs @@ -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(); diff --git a/framework-crates/objc2-foundation/src/dictionary.rs b/framework-crates/objc2-foundation/src/dictionary.rs index 72a4e83f2..a971b4993 100644 --- a/framework-crates/objc2-foundation/src/dictionary.rs +++ b/framework-crates/objc2-foundation/src/dictionary.rs @@ -381,7 +381,6 @@ impl NSMutableDictionary, { - let key = ProtocolObject::from_ref(key); // SAFETY: The key is copied, and then has the correct type `KeyType`. unsafe { self.setObject_forKey(object, key) }; } diff --git a/framework-crates/objc2-metal/examples/triangle.rs b/framework-crates/objc2-metal/examples/triangle.rs index 5d8b796f4..3c8d31bef 100644 --- a/framework-crates/objc2-metal/examples/triangle.rs +++ b/framework-crates/objc2-metal/examples/triangle.rs @@ -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 @@ -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(); diff --git a/framework-crates/objc2-web-kit/examples/browser.rs b/framework-crates/objc2-web-kit/examples/browser.rs index 0d49c05f2..f7f976291 100644 --- a/framework-crates/objc2-web-kit/examples/browser.rs +++ b/framework-crates/objc2-web-kit/examples/browser.rs @@ -200,12 +200,10 @@ declare_class!( unsafe { // handle input from text field (on , 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 @@ -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();