Skip to content

Commit 3449797

Browse files
committed
Use impl instead of ProtocolObject where possible
1 parent 63d42bf commit 3449797

File tree

6 files changed

+33
-14
lines changed

6 files changed

+33
-14
lines changed

crates/header-translator/src/rust_type.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1635,6 +1635,26 @@ impl Ty {
16351635

16361636
pub(crate) fn fn_argument(&self) -> impl fmt::Display + '_ {
16371637
FormatterFn(move |f| match self {
1638+
Inner::Id {
1639+
ty: IdType::AnyObject { protocols },
1640+
is_const: false,
1641+
lifetime: Lifetime::Unspecified | Lifetime::Strong,
1642+
nullability,
1643+
} if self.kind == TyKind::MethodArgument && !protocols.is_empty() => {
1644+
if *nullability != Nullability::NonNull {
1645+
write!(f, "Option<")?;
1646+
}
1647+
write!(f, "&")?;
1648+
write!(f, "(impl ")?;
1649+
for protocol in protocols {
1650+
write!(f, "{} + ", protocol.path())?;
1651+
}
1652+
write!(f, "Message)")?;
1653+
if *nullability != Nullability::NonNull {
1654+
write!(f, ">")?;
1655+
}
1656+
Ok(())
1657+
}
16381658
Self::Pointer {
16391659
nullability,
16401660
is_const: _,

crates/test-ui/ui/declare_class_delegate_not_mainthreadonly.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ extern_methods!(
4949
}
5050
);
5151

52-
fn main() {}
52+
fn main() {
53+
let mtm = MainThreadMarker::new().unwrap();
54+
let app = NSApplication::sharedApplication(mtm);
55+
56+
let delegate = CustomObject::new(mtm);
57+
app.setDelegate(Some(&delegate));
58+
}

framework-crates/objc2-app-kit/examples/delegate.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ fn main() {
7676

7777
// configure the application delegate
7878
let delegate = AppDelegate::new(42, true, mtm);
79-
let object = ProtocolObject::from_ref(&*delegate);
80-
app.setDelegate(Some(object));
79+
app.setDelegate(Some(&*delegate));
8180

8281
// run the app
8382
app.run();

framework-crates/objc2-foundation/src/dictionary.rs

-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,6 @@ impl<KeyType: Message, ObjectType: Message> NSMutableDictionary<KeyType, ObjectT
381381
where
382382
CopiedKey: Message + NSCopying + CopyingHelper<Result = KeyType>,
383383
{
384-
let key = ProtocolObject::from_ref(key);
385384
// SAFETY: The key is copied, and then has the correct type `KeyType`.
386385
unsafe { self.setObject_forKey(object, key) };
387386
}

framework-crates/objc2-metal/examples/triangle.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@ declare_class!(
163163

164164
// configure the metal view delegate
165165
unsafe {
166-
let object = ProtocolObject::from_ref(self);
167-
mtk_view.setDelegate(Some(object));
166+
mtk_view.setDelegate(Some(self));
168167
}
169168

170169
// configure the window
@@ -310,8 +309,7 @@ fn main() {
310309

311310
// configure the application delegate
312311
let delegate = Delegate::new(mtm);
313-
let object = ProtocolObject::from_ref(&*delegate);
314-
app.setDelegate(Some(object));
312+
app.setDelegate(Some(&*delegate));
315313

316314
// run the app
317315
app.run();

framework-crates/objc2-web-kit/examples/browser.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,10 @@ declare_class!(
200200

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

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

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

312310
// configure the application delegate
313311
let delegate = Delegate::new(mtm);
314-
let object = ProtocolObject::from_ref(&*delegate);
315-
app.setDelegate(Some(object));
312+
app.setDelegate(Some(&*delegate));
316313

317314
// run the app
318315
app.run();

0 commit comments

Comments
 (0)