Skip to content

Commit 6be088f

Browse files
committed
Remove NSCopying/NSMutableCopying workarounds in header translator
1 parent d4cd63b commit 6be088f

File tree

5 files changed

+27
-39
lines changed

5 files changed

+27
-39
lines changed

crates/header-translator/src/rust_type.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,10 @@ enum IdType {
186186
}
187187

188188
impl IdType {
189-
#[allow(dead_code)]
190189
fn _id(&self) -> Option<&ItemIdentifier> {
191190
match self {
192191
Self::Class { id, .. } => Some(id),
193192
Self::AnyObject { protocols } => match &**protocols {
194-
[id] if id.name == "NSCopying" || id.name == "NSMutableCopying" => None,
195193
[id] => Some(id),
196194
_ => None,
197195
},
@@ -370,9 +368,6 @@ impl fmt::Display for IdType {
370368
Self::AnyObject { protocols } => match &**protocols {
371369
[] => write!(f, "AnyObject"),
372370
[id] if id.is_nsobject() => write!(f, "NSObject"),
373-
[id] if id.name == "NSCopying" || id.name == "NSMutableCopying" => {
374-
write!(f, "AnyObject")
375-
}
376371
[id] => write!(f, "ProtocolObject<dyn {}>", id.path()),
377372
// TODO: Handle this better
378373
_ => write!(f, "TodoProtocols"),

crates/header-translator/src/stmt.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,13 +1680,7 @@ impl fmt::Display for Stmt {
16801680

16811681
write!(f, " pub unsafe trait {}", id.name)?;
16821682
if !protocols.is_empty() {
1683-
for (i, protocol) in protocols
1684-
.iter()
1685-
.filter(|protocol| {
1686-
protocol.name != "NSCopying" && protocol.name != "NSMutableCopying"
1687-
})
1688-
.enumerate()
1689-
{
1683+
for (i, protocol) in protocols.iter().enumerate() {
16901684
if i == 0 {
16911685
write!(f, ": ")?;
16921686
} else {

crates/icrate/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
7171
`NSTextAttachmentCellProtocol` and `NSFileProviderItemProtocol`.
7272
* **BREAKING**: Generic types no longer strictly require `Message` (although
7373
most of their trait implementations still require that).
74+
* **BREAKING**: Removed a workaround that made the `NSCopying` and
75+
`NSMutableCopying` protocols not act as regular protocols (many methods used
76+
`AnyObject` instead of the correct `ProtocolObject<dyn NSCopying>`).
7477

7578

7679
## icrate 0.0.4 - 2023-07-31

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

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ use crate::common::*;
1919
use crate::Foundation::NSMutableDictionary;
2020
use crate::Foundation::{self, NSCopying, NSDictionary};
2121

22+
fn keys_to_ptr<Q>(keys: &[&Q]) -> *mut NonNull<ProtocolObject<dyn NSCopying>>
23+
where
24+
Q: Message + NSCopying,
25+
{
26+
let keys: *mut NonNull<Q> = util::ref_ptr_cast_const(keys.as_ptr());
27+
// SAFETY: `Q` is `Message + NSCopying`, and is therefore safe to cast to
28+
// `ProtocolObject<dyn NSCopying>`.
29+
let keys: *mut NonNull<ProtocolObject<dyn NSCopying>> = keys.cast();
30+
keys
31+
}
32+
2233
impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSDictionary<K, V> {
2334
pub fn from_vec<Q>(keys: &[&Q], mut objects: Vec<Id<V>>) -> Id<Self>
2435
where
@@ -31,8 +42,7 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSDictionary<K, V> {
3142
// different lengths, either would be fine.
3243
let count = min(keys.len(), objects.len());
3344

34-
let keys: *mut NonNull<Q> = util::ref_ptr_cast_const(keys.as_ptr());
35-
let keys: *mut NonNull<AnyObject> = keys.cast();
45+
let keys = keys_to_ptr(keys);
3646
let objects = util::id_ptr_cast(objects.as_mut_ptr());
3747

3848
// SAFETY:
@@ -62,8 +72,7 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSDictionary<K, V> {
6272
{
6373
let count = min(keys.len(), objects.len());
6474

65-
let keys: *mut NonNull<Q> = util::ref_ptr_cast_const(keys.as_ptr());
66-
let keys: *mut NonNull<AnyObject> = keys.cast();
75+
let keys = keys_to_ptr(keys);
6776
let objects = util::id_ptr_cast_const(objects.as_ptr());
6877

6978
// SAFETY: See `NSDictionary::from_vec` and `NSArray::from_id_slice`.
@@ -77,8 +86,7 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSDictionary<K, V> {
7786
{
7887
let count = min(keys.len(), objects.len());
7988

80-
let keys: *mut NonNull<Q> = util::ref_ptr_cast_const(keys.as_ptr());
81-
let keys: *mut NonNull<AnyObject> = keys.cast();
89+
let keys = keys_to_ptr(keys);
8290
let objects = util::ref_ptr_cast_const(objects.as_ptr());
8391

8492
// SAFETY: See `NSDictionary::from_vec` and `NSArray::from_slice`.
@@ -95,7 +103,7 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSMutableDictionary<K,
95103
let count = min(keys.len(), objects.len());
96104

97105
let keys: *mut NonNull<Q> = util::ref_ptr_cast_const(keys.as_ptr());
98-
let keys: *mut NonNull<AnyObject> = keys.cast();
106+
let keys: *mut NonNull<ProtocolObject<dyn NSCopying>> = keys.cast();
99107
let objects = util::id_ptr_cast(objects.as_mut_ptr());
100108

101109
// SAFETY: See `NSDictionary::from_vec`
@@ -109,8 +117,7 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSMutableDictionary<K,
109117
{
110118
let count = min(keys.len(), objects.len());
111119

112-
let keys: *mut NonNull<Q> = util::ref_ptr_cast_const(keys.as_ptr());
113-
let keys: *mut NonNull<AnyObject> = keys.cast();
120+
let keys = keys_to_ptr(keys);
114121
let objects = util::id_ptr_cast_const(objects.as_ptr());
115122

116123
// SAFETY: See `NSDictionary::from_vec` and `NSArray::from_id_slice`.
@@ -124,8 +131,7 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSMutableDictionary<K,
124131
{
125132
let count = min(keys.len(), objects.len());
126133

127-
let keys: *mut NonNull<Q> = util::ref_ptr_cast_const(keys.as_ptr());
128-
let keys: *mut NonNull<AnyObject> = keys.cast();
134+
let keys = keys_to_ptr(keys);
129135
let objects = util::ref_ptr_cast_const(objects.as_ptr());
130136

131137
// SAFETY: See `NSDictionary::from_vec` and `NSArray::from_slice`.
@@ -335,13 +341,8 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSMutableDictionary<K,
335341
.get(key)
336342
.map(|old_obj| unsafe { util::mutable_collection_retain_removed_id(old_obj) });
337343

338-
// SAFETY: It is always safe to transmute an `&T` where `T: Message`
339-
// to `&AnyObject`.
340-
let key: NonNull<K> = NonNull::from(key);
341-
let key: NonNull<AnyObject> = key.cast();
342-
let key: &AnyObject = unsafe { key.as_ref() };
343-
// SAFETY: The key is NSCopying (see `NSDictionary::from_vec`), and we
344-
// have ownership over the value.
344+
let key = ProtocolObject::from_ref(key);
345+
// SAFETY: We have ownership over the value.
345346
unsafe { self.setObject_forKey(&value, key) };
346347
old_obj
347348
}
@@ -371,14 +372,9 @@ impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSMutableDictionary<K,
371372
.get(key)
372373
.map(|old_obj| unsafe { util::mutable_collection_retain_removed_id(old_obj) });
373374

374-
// SAFETY: It is always safe to transmute an `&T` where `T: Message`
375-
// to `&AnyObject`.
376-
let key: NonNull<K> = NonNull::from(key);
377-
let key: NonNull<AnyObject> = key.cast();
378-
let key: &AnyObject = unsafe { key.as_ref() };
379-
// SAFETY: The key is NSCopying (see `NSDictionary::from_vec`), and
380-
// the value is `IsRetainable` (and hence safe for the collection to
381-
// retain).
375+
let key = ProtocolObject::from_ref(key);
376+
// SAFETY: The value is `IsRetainable`, and hence safe for the
377+
// collection to retain.
382378
unsafe { self.setObject_forKey(value, key) };
383379
old_obj
384380
}

crates/icrate/src/generated

0 commit comments

Comments
 (0)