Skip to content

Commit 83b4a73

Browse files
committed
Make generic types not strictly require T: Message
1 parent 9351cf4 commit 83b4a73

File tree

9 files changed

+67
-173
lines changed

9 files changed

+67
-173
lines changed

crates/header-translator/src/stmt.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ impl fmt::Display for Stmt {
13811381
if !generics.is_empty() {
13821382
write!(f, "<")?;
13831383
for generic in generics {
1384-
write!(f, "{generic}: Message = AnyObject, ")?;
1384+
write!(f, "{generic}: ?Sized = AnyObject, ")?;
13851385
}
13861386
write!(f, ">")?;
13871387
};
@@ -1411,7 +1411,7 @@ impl fmt::Display for Stmt {
14111411
writeln!(
14121412
f,
14131413
" unsafe impl{} ClassType for {}{} {{",
1414-
GenericParamsHelper(generics, "Message"),
1414+
GenericParamsHelper(generics, "?Sized + Message"),
14151415
id.name,
14161416
GenericTyHelper(generics),
14171417
)?;
@@ -1494,6 +1494,7 @@ impl fmt::Display for Stmt {
14941494
if let Some(feature) = cls.feature() {
14951495
writeln!(f, " #[cfg(feature = \"{feature}\")]")?;
14961496
}
1497+
// TODO: Add ?Sized here once `extern_methods!` supports it.
14971498
writeln!(
14981499
f,
14991500
" unsafe impl{} {}{} {{",
@@ -1545,6 +1546,7 @@ impl fmt::Display for Stmt {
15451546
writeln!(f, ");")?;
15461547

15471548
if let Some(method) = methods.iter().find(|method| method.usable_in_default_id()) {
1549+
writeln!(f)?;
15481550
if let Some(feature) = cls.feature() {
15491551
// Assume new methods require no extra features
15501552
writeln!(f, " #[cfg(feature = \"{feature}\")]")?;
@@ -1574,11 +1576,11 @@ impl fmt::Display for Stmt {
15741576
// The object inherits from `NSObject` or `NSProxy` no
15751577
// matter what the generic type is, so this must be
15761578
// safe.
1577-
(_, _) if protocol.is_nsobject() => ("Message", None),
1579+
(_, _) if protocol.is_nsobject() => ("?Sized", None),
15781580
// Encoding and decoding requires that the inner types
15791581
// are codable as well.
1580-
("Foundation", "NSCoding") => ("Message + NSCoding", None),
1581-
("Foundation", "NSSecureCoding") => ("Message + NSSecureCoding", None),
1582+
("Foundation", "NSCoding") => ("?Sized + NSCoding", None),
1583+
("Foundation", "NSSecureCoding") => ("?Sized + NSSecureCoding", None),
15821584
// Copying collections is done as a shallow copy:
15831585
// <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Collections/Articles/Copying.html>
15841586
//
@@ -1588,24 +1590,24 @@ impl fmt::Display for Stmt {
15881590
//
15891591
// The types does have to be cloneable, since generic
15901592
// types effectively store an `Id<T>` of the type.
1591-
("Foundation", "NSCopying") => ("IsIdCloneable", None),
1592-
("Foundation", "NSMutableCopying") => ("IsIdCloneable", None),
1593+
("Foundation", "NSCopying") => ("?Sized + IsIdCloneable", None),
1594+
("Foundation", "NSMutableCopying") => ("?Sized + IsIdCloneable", None),
15931595
// TODO: Do we need further tweaks to this?
1594-
("Foundation", "NSFastEnumeration") => ("Message", None),
1596+
("Foundation", "NSFastEnumeration") => ("?Sized", None),
15951597
// AppKit fixes. TODO: Should we add more bounds here?
1596-
("AppKit", "NSCollectionViewDataSource") => ("Message", None),
1597-
("AppKit", "NSTableViewDataSource") => ("Message", None),
1598+
("AppKit", "NSCollectionViewDataSource") => ("?Sized", None),
1599+
("AppKit", "NSTableViewDataSource") => ("?Sized", None),
15981600
_ => {
15991601
error!(
16001602
?protocol,
16011603
?cls,
16021604
"unknown where bound for generic protocol impl"
16031605
);
1604-
("Message", None)
1606+
("?Sized", None)
16051607
}
16061608
}
16071609
} else {
1608-
("Message", None)
1610+
("InvalidGenericBound", None)
16091611
};
16101612

16111613
if let Some(feature) = cls.feature() {

crates/icrate/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
6767
`NSSet` creation methods, fixing a long-standing soundess issue.
6868
* Fixed the protocol names of `NSAccessibilityElementProtocol`,
6969
`NSTextAttachmentCellProtocol` and `NSFileProviderItemProtocol`.
70+
* **BREAKING**: Generic types no longer strictly require `Message` (although
71+
most of their trait implementations still require that).
7072

7173

7274
## icrate 0.0.4 - 2023-07-31

crates/icrate/src/fixes/AppKit/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ extern_class!(
4343
__inner_extern_class!(
4444
#[cfg(feature = "AppKit_NSLayoutAnchor")]
4545
#[derive(Debug, PartialEq, Eq, Hash)]
46-
pub struct NSLayoutAnchor<AnchorType: Message = AnyObject> {
46+
pub struct NSLayoutAnchor<AnchorType: ?Sized = AnyObject> {
4747
__superclass: NSObject,
4848
_inner0: PhantomData<*mut AnchorType>,
4949
notunwindsafe: PhantomData<&'static mut ()>,
5050
}
5151

5252
#[cfg(feature = "AppKit_NSLayoutAnchor")]
53-
unsafe impl<AnchorType: Message> ClassType for NSLayoutAnchor<AnchorType> {
53+
unsafe impl<AnchorType: ?Sized + Message> ClassType for NSLayoutAnchor<AnchorType> {
5454
type Super = NSObject;
5555
type Mutability = InteriorMutable;
5656

crates/icrate/src/fixes/Foundation/debug.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ impl<K: fmt::Debug + Message, V: fmt::Debug + Message> fmt::Debug
5353
}
5454
}
5555

56+
#[cfg(feature = "Foundation_NSCountedSet")]
57+
impl<T: fmt::Debug + Message> fmt::Debug for Foundation::NSCountedSet<T> {
58+
#[inline]
59+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60+
fmt::Debug::fmt(&**self, f)
61+
}
62+
}
63+
5664
#[cfg(feature = "Foundation_NSMutableSet")]
5765
impl<T: fmt::Debug + Message> fmt::Debug for Foundation::NSMutableSet<T> {
5866
#[inline]

crates/icrate/src/fixes/Foundation/generics.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl<T: ?Sized> UnwindSafe for UnsafeIgnoreAutoTraits<T> {}
1818
__inner_extern_class!(
1919
#[derive(PartialEq, Eq, Hash)]
2020
#[cfg(feature = "Foundation_NSArray")]
21-
pub struct NSArray<ObjectType: Message = AnyObject> {
21+
pub struct NSArray<ObjectType: ?Sized = AnyObject> {
2222
// SAFETY: Auto traits specified below.
2323
__superclass: UnsafeIgnoreAutoTraits<NSObject>,
2424
/// `NSArray` and `NSMutableArray` have `Id`-like storage.
@@ -84,7 +84,7 @@ __inner_extern_class!(
8484
}
8585

8686
#[cfg(feature = "Foundation_NSArray")]
87-
unsafe impl<ObjectType: Message> ClassType for NSArray<ObjectType> {
87+
unsafe impl<ObjectType: ?Sized + Message> ClassType for NSArray<ObjectType> {
8888
type Super = NSObject;
8989
type Mutability = ImmutableWithMutableSubclass<NSMutableArray<ObjectType>>;
9090

@@ -101,13 +101,13 @@ __inner_extern_class!(
101101
__inner_extern_class!(
102102
#[derive(PartialEq, Eq, Hash)]
103103
#[cfg(feature = "Foundation_NSArray")]
104-
pub struct NSMutableArray<ObjectType: Message = AnyObject> {
104+
pub struct NSMutableArray<ObjectType: ?Sized = AnyObject> {
105105
// Inherit auto traits from superclass.
106106
__superclass: NSArray<ObjectType>,
107107
}
108108

109109
#[cfg(feature = "Foundation_NSArray")]
110-
unsafe impl<ObjectType: Message> ClassType for NSMutableArray<ObjectType> {
110+
unsafe impl<ObjectType: ?Sized + Message> ClassType for NSMutableArray<ObjectType> {
111111
#[inherits(NSObject)]
112112
type Super = NSArray<ObjectType>;
113113
type Mutability = MutableWithImmutableSuperclass<NSArray<ObjectType>>;
@@ -125,7 +125,7 @@ __inner_extern_class!(
125125
__inner_extern_class!(
126126
#[derive(PartialEq, Eq, Hash)]
127127
#[cfg(feature = "Foundation_NSDictionary")]
128-
pub struct NSDictionary<KeyType: Message = AnyObject, ObjectType: Message = AnyObject> {
128+
pub struct NSDictionary<KeyType: ?Sized = AnyObject, ObjectType: ?Sized = AnyObject> {
129129
// SAFETY: Auto traits specified below.
130130
__superclass: UnsafeIgnoreAutoTraits<NSObject>,
131131
// Same as if the dictionary was implemented with:
@@ -134,7 +134,9 @@ __inner_extern_class!(
134134
}
135135

136136
#[cfg(feature = "Foundation_NSDictionary")]
137-
unsafe impl<KeyType: Message, ObjectType: Message> ClassType for NSDictionary<KeyType, ObjectType> {
137+
unsafe impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> ClassType
138+
for NSDictionary<KeyType, ObjectType>
139+
{
138140
type Super = NSObject;
139141
type Mutability = ImmutableWithMutableSubclass<NSMutableDictionary<KeyType, ObjectType>>;
140142

@@ -151,13 +153,13 @@ __inner_extern_class!(
151153
__inner_extern_class!(
152154
#[derive(PartialEq, Eq, Hash)]
153155
#[cfg(feature = "Foundation_NSDictionary")]
154-
pub struct NSMutableDictionary<KeyType: Message = AnyObject, ObjectType: Message = AnyObject> {
156+
pub struct NSMutableDictionary<KeyType: ?Sized = AnyObject, ObjectType: ?Sized = AnyObject> {
155157
// Inherit auto traits from superclass.
156158
__superclass: NSDictionary<KeyType, ObjectType>,
157159
}
158160

159161
#[cfg(feature = "Foundation_NSDictionary")]
160-
unsafe impl<KeyType: Message, ObjectType: Message> ClassType
162+
unsafe impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> ClassType
161163
for NSMutableDictionary<KeyType, ObjectType>
162164
{
163165
#[inherits(NSObject)]
@@ -177,15 +179,15 @@ __inner_extern_class!(
177179
__inner_extern_class!(
178180
#[derive(PartialEq, Eq, Hash)]
179181
#[cfg(feature = "Foundation_NSSet")]
180-
pub struct NSSet<ObjectType: Message = AnyObject> {
182+
pub struct NSSet<ObjectType: ?Sized = AnyObject> {
181183
// SAFETY: Auto traits specified below.
182184
__superclass: UnsafeIgnoreAutoTraits<NSObject>,
183185
// Same as if the set was implemented as `NSArray<ObjectType>`.
184186
__inner: PhantomData<Id<ObjectType>>,
185187
}
186188

187189
#[cfg(feature = "Foundation_NSSet")]
188-
unsafe impl<ObjectType: Message> ClassType for NSSet<ObjectType> {
190+
unsafe impl<ObjectType: ?Sized + Message> ClassType for NSSet<ObjectType> {
189191
type Super = NSObject;
190192
type Mutability = ImmutableWithMutableSubclass<NSMutableSet<ObjectType>>;
191193

@@ -202,13 +204,13 @@ __inner_extern_class!(
202204
__inner_extern_class!(
203205
#[derive(PartialEq, Eq, Hash)]
204206
#[cfg(feature = "Foundation_NSSet")]
205-
pub struct NSMutableSet<ObjectType: Message = AnyObject> {
207+
pub struct NSMutableSet<ObjectType: ?Sized = AnyObject> {
206208
// Inherit auto traits from superclass.
207209
__superclass: NSSet<ObjectType>,
208210
}
209211

210212
#[cfg(feature = "Foundation_NSSet")]
211-
unsafe impl<ObjectType: Message> ClassType for NSMutableSet<ObjectType> {
213+
unsafe impl<ObjectType: ?Sized + Message> ClassType for NSMutableSet<ObjectType> {
212214
#[inherits(NSObject)]
213215
type Super = NSSet<ObjectType>;
214216
type Mutability = MutableWithImmutableSuperclass<NSSet<ObjectType>>;
@@ -224,15 +226,15 @@ __inner_extern_class!(
224226
);
225227

226228
__inner_extern_class!(
227-
#[derive(Debug, PartialEq, Eq, Hash)]
229+
#[derive(PartialEq, Eq, Hash)]
228230
#[cfg(feature = "Foundation_NSCountedSet")]
229-
pub struct NSCountedSet<ObjectType: Message = AnyObject> {
231+
pub struct NSCountedSet<ObjectType: ?Sized = AnyObject> {
230232
// Inherit auto traits from superclass.
231233
__superclass: NSMutableSet<ObjectType>,
232234
}
233235

234236
#[cfg(feature = "Foundation_NSCountedSet")]
235-
unsafe impl<ObjectType: Message> ClassType for NSCountedSet<ObjectType> {
237+
unsafe impl<ObjectType: ?Sized + Message> ClassType for NSCountedSet<ObjectType> {
236238
#[inherits(NSSet<ObjectType>, NSObject)]
237239
type Super = NSMutableSet<ObjectType>;
238240
type Mutability = Mutable;
@@ -250,15 +252,15 @@ __inner_extern_class!(
250252
__inner_extern_class!(
251253
#[derive(PartialEq, Eq, Hash)]
252254
#[cfg(feature = "Foundation_NSOrderedSet")]
253-
pub struct NSOrderedSet<ObjectType: Message = AnyObject> {
255+
pub struct NSOrderedSet<ObjectType: ?Sized = AnyObject> {
254256
// SAFETY: Auto traits specified below.
255257
__superclass: UnsafeIgnoreAutoTraits<NSObject>,
256258
// Same as if the set was implemented with `NSArray<ObjectType>`.
257259
__inner: PhantomData<Id<ObjectType>>,
258260
}
259261

260262
#[cfg(feature = "Foundation_NSOrderedSet")]
261-
unsafe impl<ObjectType: Message> ClassType for NSOrderedSet<ObjectType> {
263+
unsafe impl<ObjectType: ?Sized + Message> ClassType for NSOrderedSet<ObjectType> {
262264
type Super = NSObject;
263265
type Mutability = ImmutableWithMutableSubclass<NSMutableOrderedSet<ObjectType>>;
264266

@@ -275,13 +277,13 @@ __inner_extern_class!(
275277
__inner_extern_class!(
276278
#[derive(PartialEq, Eq, Hash)]
277279
#[cfg(feature = "Foundation_NSOrderedSet")]
278-
pub struct NSMutableOrderedSet<ObjectType: Message = AnyObject> {
280+
pub struct NSMutableOrderedSet<ObjectType: ?Sized = AnyObject> {
279281
// Inherit auto traits from superclass.
280282
__superclass: NSOrderedSet<ObjectType>,
281283
}
282284

283285
#[cfg(feature = "Foundation_NSOrderedSet")]
284-
unsafe impl<ObjectType: Message> ClassType for NSMutableOrderedSet<ObjectType> {
286+
unsafe impl<ObjectType: ?Sized + Message> ClassType for NSMutableOrderedSet<ObjectType> {
285287
#[inherits(NSObject)]
286288
type Super = NSOrderedSet<ObjectType>;
287289
type Mutability = MutableWithImmutableSuperclass<NSOrderedSet<ObjectType>>;
@@ -299,7 +301,7 @@ __inner_extern_class!(
299301
__inner_extern_class!(
300302
#[derive(Debug, PartialEq, Eq, Hash)]
301303
#[cfg(feature = "Foundation_NSEnumerator")]
302-
pub struct NSEnumerator<ObjectType: Message = AnyObject> {
304+
pub struct NSEnumerator<ObjectType: ?Sized = AnyObject> {
303305
// SAFETY: Auto traits specified below.
304306
__superclass: UnsafeIgnoreAutoTraits<NSObject>,
305307
// Enumerators are basically the same as if we were storing
@@ -315,7 +317,7 @@ __inner_extern_class!(
315317
}
316318

317319
#[cfg(feature = "Foundation_NSEnumerator")]
318-
unsafe impl<ObjectType: Message> ClassType for NSEnumerator<ObjectType> {
320+
unsafe impl<ObjectType: ?Sized + Message> ClassType for NSEnumerator<ObjectType> {
319321
type Super = NSObject;
320322
type Mutability = Mutable;
321323

crates/icrate/src/generated

crates/objc2/src/macros/extern_class.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,13 @@ macro_rules! __impl_as_ref_borrow {
321321
macro_rules! __inner_extern_class {
322322
(
323323
$(#[$m:meta])*
324-
$v:vis struct $name:ident<$($t_struct:ident $(: $b_struct:ident $(= $default:ty)?)?),* $(,)?> {
324+
$v:vis struct $name:ident<$($t_struct:ident $(: $(?$b_sized_struct:ident)? $($b_struct:ident)? $(= $default:ty)?)?),* $(,)?> {
325325
$superclass_field:ident: $superclass_field_ty:ty,
326326
$($fields:tt)*
327327
}
328328

329329
$(#[$impl_m:meta])*
330-
unsafe impl<$($t_for:ident $(: $b_for:ident)?),* $(,)?> ClassType for $for:ty {
330+
unsafe impl<$($t_for:ident $(: $(?$b_sized_for:ident +)? $b_for:ident)?),* $(,)?> ClassType for $for:ty {
331331
$(#[inherits($($inheritance_rest:ty),+ $(,)?)])?
332332
type Super = $superclass:ty;
333333
type Mutability = $mutability:ty;
@@ -341,7 +341,7 @@ macro_rules! __inner_extern_class {
341341
$crate::__emit_struct! {
342342
($(#[$m])*)
343343
($v)
344-
($name<$($t_struct $(: $b_struct $(= $default)?)?),*>)
344+
($name<$($t_struct $(: $(?$b_sized_struct)? $($b_struct)? $(= $default)?)?),*>)
345345
(
346346
$superclass_field: $superclass_field_ty,
347347
$($fields)*
@@ -350,7 +350,7 @@ macro_rules! __inner_extern_class {
350350

351351
$crate::__extern_class_impl_traits! {
352352
$(#[$impl_m])*
353-
unsafe impl ($($t_for $(: $b_for)?),*) for $for {
353+
unsafe impl ($($t_for $(: $(?$b_sized_for +)? $b_for)?),*) for $for {
354354
INHERITS = [$superclass, $($($inheritance_rest,)+)? $crate::runtime::AnyObject];
355355

356356
fn as_super(&$as_super_self) $as_super
@@ -359,7 +359,7 @@ macro_rules! __inner_extern_class {
359359
}
360360

361361
$(#[$impl_m])*
362-
unsafe impl<$($t_for $(: $b_for)?),*> ClassType for $for {
362+
unsafe impl<$($t_for $(: $(?$b_sized_for +)? $b_for)?),*> ClassType for $for {
363363
type Super = $superclass;
364364
type Mutability = $mutability;
365365
const NAME: &'static $crate::__macro_helpers::str = $crate::__select_name!($name; $($name_const)?);

0 commit comments

Comments
 (0)