Skip to content

Commit 392f711

Browse files
committed
Use impl instead of ProtocolObject where possible
1 parent 6d75180 commit 392f711

File tree

7 files changed

+29
-19
lines changed

7 files changed

+29
-19
lines changed

crates/header-translator/src/rust_type.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1758,6 +1758,26 @@ impl fmt::Display for Ty {
17581758
ty => write!(f, "{ty}"),
17591759
},
17601760
TyKind::MethodArgument | TyKind::FnArgument => match &self.ty {
1761+
Inner::Id {
1762+
ty: IdType::AnyObject { protocols },
1763+
is_const: false,
1764+
lifetime: Lifetime::Unspecified | Lifetime::Strong,
1765+
nullability,
1766+
} if self.kind == TyKind::MethodArgument && !protocols.is_empty() => {
1767+
if *nullability != Nullability::NonNull {
1768+
write!(f, "Option<")?;
1769+
}
1770+
write!(f, "&")?;
1771+
write!(f, "(impl ")?;
1772+
for protocol in protocols {
1773+
write!(f, "{} + ", protocol.path())?;
1774+
}
1775+
write!(f, "Message)")?;
1776+
if *nullability != Nullability::NonNull {
1777+
write!(f, ">")?;
1778+
}
1779+
Ok(())
1780+
}
17611781
Inner::Id {
17621782
ty,
17631783
is_const: false,

crates/icrate/examples/browser.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use objc2::{
2222
declare_class, msg_send, msg_send_id,
2323
mutability::MainThreadOnly,
2424
rc::Id,
25-
runtime::{AnyObject, ProtocolObject, Sel},
25+
runtime::{AnyObject, Sel},
2626
sel, ClassType,
2727
};
2828

@@ -197,12 +197,10 @@ declare_class!(
197197

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

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

208206
// create the menu with a "quit" entry
@@ -304,8 +302,7 @@ fn main() {
304302
let delegate = Delegate::new(mtm);
305303

306304
// configure the application delegate
307-
let object = ProtocolObject::from_ref(&*delegate);
308-
app.setDelegate(Some(object));
305+
app.setDelegate(Some(&*delegate));
309306

310307
// run the app
311308
unsafe { app.run() };

crates/icrate/examples/delegate.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use icrate::Foundation::{
77
};
88
use objc2::declare::{Ivar, IvarBool, IvarDrop, IvarEncode};
99
use objc2::rc::Id;
10-
use objc2::runtime::ProtocolObject;
1110
use objc2::{declare_class, msg_send, msg_send_id, mutability, ClassType};
1211

1312
declare_class!(
@@ -85,8 +84,7 @@ fn main() {
8584
println!("{delegate:?}");
8685

8786
// configure the application delegate
88-
let object = ProtocolObject::from_ref(&*delegate);
89-
app.setDelegate(Some(object));
87+
app.setDelegate(Some(&delegate));
9088

9189
// run the app
9290
unsafe { app.run() };

crates/icrate/examples/metal.rs

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

230230
// configure the metal view delegate
231231
unsafe {
232-
let object = ProtocolObject::from_ref(self);
233-
mtk_view.setDelegate(Some(object));
232+
mtk_view.setDelegate(Some(self));
234233
}
235234

236235
// configure the window
@@ -374,8 +373,7 @@ fn main() {
374373
let delegate = Delegate::new(mtm);
375374

376375
// configure the application delegate
377-
let object = ProtocolObject::from_ref(&*delegate);
378-
app.setDelegate(Some(object));
376+
app.setDelegate(Some(&delegate));
379377

380378
// run the app
381379
unsafe { app.run() };

crates/icrate/src/additions/Foundation/dictionary.rs

-2
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,6 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSMutableDictionary<K,
341341
.get(key)
342342
.map(|old_obj| unsafe { util::mutable_collection_retain_removed_id(old_obj) });
343343

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

375-
let key = ProtocolObject::from_ref(key);
376374
// SAFETY: The value is `IsRetainable`, and hence safe for the
377375
// collection to retain.
378376
unsafe { self.setObject_forKey(value, key) };

crates/icrate/src/generated

crates/test-ui/ui/declare_class_delegate_not_mainthreadonly.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,5 @@ fn main() {
3838
let app = NSApplication::sharedApplication(mtm);
3939

4040
let delegate = CustomObject::new(mtm);
41-
let delegate = ProtocolObject::from_ref(&*delegate);
42-
app.setDelegate(Some(delegate));
41+
app.setDelegate(Some(&delegate));
4342
}

0 commit comments

Comments
 (0)