1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#![allow(dead_code, unused_imports)]
use core::marker::PhantomData;
use core::panic::{RefUnwindSafe, UnwindSafe};

use objc2::rc::Retained;
use objc2::runtime::{AnyObject, NSObject};
use objc2::{__inner_extern_class, mutability, ClassType, Message};

#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Clone, Copy, Debug)]
#[repr(transparent)]
struct UnsafeIgnoreAutoTraits<T: ?Sized>(T);
// SAFETY: Moved to the name of the struct
unsafe impl<T: ?Sized> Send for UnsafeIgnoreAutoTraits<T> {}
// SAFETY: Moved to the name of the struct
unsafe impl<T: ?Sized> Sync for UnsafeIgnoreAutoTraits<T> {}
impl<T: ?Sized> RefUnwindSafe for UnsafeIgnoreAutoTraits<T> {}
impl<T: ?Sized> UnwindSafe for UnsafeIgnoreAutoTraits<T> {}
// TODO
// impl<T: ?Sized> Unpin for UnsafeIgnoreAutoTraits<T> {}

__inner_extern_class!(
    #[derive(PartialEq, Eq, Hash)]
    #[cfg(feature = "NSArray")]
    pub struct NSArray<ObjectType: ?Sized = AnyObject> {
        // SAFETY: Auto traits specified below.
        __superclass: UnsafeIgnoreAutoTraits<NSObject>,
        /// `NSArray` and `NSMutableArray` have `Retained`-like storage.
        ///
        /// This fairly trivially true for `NSMutableArray<T: IsMutable>` and
        /// `NSArray<T: IsIdCloneable>`, but let's explore the two other cases
        /// a bit:
        ///
        ///
        /// # `NSMutableArray<T: IsIdCloneable>`
        ///
        /// Must have `Arc`-like auto traits, since `NSMutableArray<T>` is
        /// `NSMutableCopying` in that case (which is a shallow clone).
        ///
        /// E.g. if we had `NSMutableArray<T: Send>: Send`, `T` would be
        /// accessible from multiple threads, therefore we require `T: Sync`:
        /// ```ignore
        /// std::thread::scope(|s| {
        ///     let obj: Retained<NSMutableArray<T>>;
        ///     let obj2 = obj.mutableCopy();
        ///     s.spawn(move || {
        ///         let obj = obj;
        ///     });
        ///     s.spawn(move || {
        ///         let obj2 = obj2;
        ///     });
        /// })
        /// ```
        ///
        /// Similarly, if we had `NSMutableArray<T: Sync>: Sync`, `T` would be
        /// dropable from other threads, therefore we require `T: Send`:
        /// ```ignore
        /// let obj: Retained<NSMutableArray<T>>;
        /// std::thread::spawn(|| {
        ///     let obj2 = obj.mutableCopy();
        ///
        ///     // Do some long-lived work, the original `obj` is dropped in
        ///     // the original thread in the meantime.
        ///
        ///     // And now, the destructor is run on the wrong thread.
        ///     drop(obj2);
        /// });
        /// // (note: example does not work due to lifetime issues, but the
        /// // idea still stands).
        /// ```
        ///
        ///
        /// # `NSArray<T: IsMutable>`
        ///
        /// This could safely be `Arc`-like, but for consistency with
        /// `NSMutableArray` (esp. since `get_mut` is available on `NSArray`
        /// and not `NSMutableArray`), we make it `Box`-like.
        ///
        /// This may seem wrong since arrays are unconditionally immutable,
        /// e.g. `ClassType::Mutability` is always
        /// `ImmutableWithMutableSubclass`.
        ///
        /// But since we already require `T: IsCloneable` on `NSCopying`, and
        /// already prevent other forms of `&NSArray<T> -> Retained<NSArray<T>>`,
        /// this is actually fine, since `Retained<NSArray<T>>: Send | Sync`
        /// requires `NSArray<T>: Send + Sync` (and hence `T: Send + Sync`).
        __inner: PhantomData<Retained<ObjectType>>,
    }

    #[cfg(feature = "NSArray")]
    unsafe impl<ObjectType: ?Sized + Message> ClassType for NSArray<ObjectType> {
        type Super = NSObject;
        type Mutability = mutability::ImmutableWithMutableSubclass<NSMutableArray<ObjectType>>;

        fn as_super(&self) -> &Self::Super {
            &self.__superclass.0
        }

        fn as_super_mut(&mut self) -> &mut Self::Super {
            &mut self.__superclass.0
        }
    }
);

__inner_extern_class!(
    #[derive(PartialEq, Eq, Hash)]
    #[cfg(feature = "NSArray")]
    pub struct NSMutableArray<ObjectType: ?Sized = AnyObject> {
        // Inherit auto traits from superclass.
        __superclass: NSArray<ObjectType>,
    }

    #[cfg(feature = "NSArray")]
    unsafe impl<ObjectType: ?Sized + Message> ClassType for NSMutableArray<ObjectType> {
        #[inherits(NSObject)]
        type Super = NSArray<ObjectType>;
        type Mutability = mutability::MutableWithImmutableSuperclass<NSArray<ObjectType>>;

        fn as_super(&self) -> &Self::Super {
            &self.__superclass
        }

        fn as_super_mut(&mut self) -> &mut Self::Super {
            &mut self.__superclass
        }
    }
);

__inner_extern_class!(
    #[derive(PartialEq, Eq, Hash)]
    #[cfg(feature = "NSDictionary")]
    pub struct NSDictionary<KeyType: ?Sized = AnyObject, ObjectType: ?Sized = AnyObject> {
        // SAFETY: Auto traits specified below.
        __superclass: UnsafeIgnoreAutoTraits<NSObject>,
        // Same as if the dictionary was implemented with:
        // `(NSArray<KeyType>, NSArray<ObjectType>)`
        __inner: PhantomData<(Retained<KeyType>, Retained<ObjectType>)>,
    }

    #[cfg(feature = "NSDictionary")]
    unsafe impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> ClassType
        for NSDictionary<KeyType, ObjectType>
    {
        type Super = NSObject;
        type Mutability =
            mutability::ImmutableWithMutableSubclass<NSMutableDictionary<KeyType, ObjectType>>;

        fn as_super(&self) -> &Self::Super {
            &self.__superclass.0
        }

        fn as_super_mut(&mut self) -> &mut Self::Super {
            &mut self.__superclass.0
        }
    }
);

__inner_extern_class!(
    #[derive(PartialEq, Eq, Hash)]
    #[cfg(feature = "NSDictionary")]
    pub struct NSMutableDictionary<KeyType: ?Sized = AnyObject, ObjectType: ?Sized = AnyObject> {
        // Inherit auto traits from superclass.
        __superclass: NSDictionary<KeyType, ObjectType>,
    }

    #[cfg(feature = "NSDictionary")]
    unsafe impl<KeyType: ?Sized + Message, ObjectType: ?Sized + Message> ClassType
        for NSMutableDictionary<KeyType, ObjectType>
    {
        #[inherits(NSObject)]
        type Super = NSDictionary<KeyType, ObjectType>;
        type Mutability =
            mutability::MutableWithImmutableSuperclass<NSDictionary<KeyType, ObjectType>>;

        fn as_super(&self) -> &Self::Super {
            &self.__superclass
        }

        fn as_super_mut(&mut self) -> &mut Self::Super {
            &mut self.__superclass
        }
    }
);

__inner_extern_class!(
    #[derive(PartialEq, Eq, Hash)]
    #[cfg(feature = "NSSet")]
    pub struct NSSet<ObjectType: ?Sized = AnyObject> {
        // SAFETY: Auto traits specified below.
        __superclass: UnsafeIgnoreAutoTraits<NSObject>,
        // Same as if the set was implemented as `NSArray<ObjectType>`.
        __inner: PhantomData<Retained<ObjectType>>,
    }

    #[cfg(feature = "NSSet")]
    unsafe impl<ObjectType: ?Sized + Message> ClassType for NSSet<ObjectType> {
        type Super = NSObject;
        type Mutability = mutability::ImmutableWithMutableSubclass<NSMutableSet<ObjectType>>;

        fn as_super(&self) -> &Self::Super {
            &self.__superclass.0
        }

        fn as_super_mut(&mut self) -> &mut Self::Super {
            &mut self.__superclass.0
        }
    }
);

__inner_extern_class!(
    #[derive(PartialEq, Eq, Hash)]
    #[cfg(feature = "NSSet")]
    pub struct NSMutableSet<ObjectType: ?Sized = AnyObject> {
        // Inherit auto traits from superclass.
        __superclass: NSSet<ObjectType>,
    }

    #[cfg(feature = "NSSet")]
    unsafe impl<ObjectType: ?Sized + Message> ClassType for NSMutableSet<ObjectType> {
        #[inherits(NSObject)]
        type Super = NSSet<ObjectType>;
        type Mutability = mutability::MutableWithImmutableSuperclass<NSSet<ObjectType>>;

        fn as_super(&self) -> &Self::Super {
            &self.__superclass
        }

        fn as_super_mut(&mut self) -> &mut Self::Super {
            &mut self.__superclass
        }
    }
);

__inner_extern_class!(
    #[derive(PartialEq, Eq, Hash)]
    #[cfg(feature = "NSSet")]
    pub struct NSCountedSet<ObjectType: ?Sized = AnyObject> {
        // Inherit auto traits from superclass.
        __superclass: NSMutableSet<ObjectType>,
    }

    #[cfg(feature = "NSSet")]
    unsafe impl<ObjectType: ?Sized + Message> ClassType for NSCountedSet<ObjectType> {
        #[inherits(NSSet<ObjectType>, NSObject)]
        type Super = NSMutableSet<ObjectType>;
        type Mutability = mutability::Mutable;

        fn as_super(&self) -> &Self::Super {
            &self.__superclass
        }

        fn as_super_mut(&mut self) -> &mut Self::Super {
            &mut self.__superclass
        }
    }
);

__inner_extern_class!(
    #[derive(PartialEq, Eq, Hash)]
    #[cfg(feature = "NSOrderedSet")]
    pub struct NSOrderedSet<ObjectType: ?Sized = AnyObject> {
        // SAFETY: Auto traits specified below.
        __superclass: UnsafeIgnoreAutoTraits<NSObject>,
        // Same as if the set was implemented with `NSArray<ObjectType>`.
        __inner: PhantomData<Retained<ObjectType>>,
    }

    #[cfg(feature = "NSOrderedSet")]
    unsafe impl<ObjectType: ?Sized + Message> ClassType for NSOrderedSet<ObjectType> {
        type Super = NSObject;
        type Mutability = mutability::ImmutableWithMutableSubclass<NSMutableOrderedSet<ObjectType>>;

        fn as_super(&self) -> &Self::Super {
            &self.__superclass.0
        }

        fn as_super_mut(&mut self) -> &mut Self::Super {
            &mut self.__superclass.0
        }
    }
);

__inner_extern_class!(
    #[derive(PartialEq, Eq, Hash)]
    #[cfg(feature = "NSOrderedSet")]
    pub struct NSMutableOrderedSet<ObjectType: ?Sized = AnyObject> {
        // Inherit auto traits from superclass.
        __superclass: NSOrderedSet<ObjectType>,
    }

    #[cfg(feature = "NSOrderedSet")]
    unsafe impl<ObjectType: ?Sized + Message> ClassType for NSMutableOrderedSet<ObjectType> {
        #[inherits(NSObject)]
        type Super = NSOrderedSet<ObjectType>;
        type Mutability = mutability::MutableWithImmutableSuperclass<NSOrderedSet<ObjectType>>;

        fn as_super(&self) -> &Self::Super {
            &self.__superclass
        }

        fn as_super_mut(&mut self) -> &mut Self::Super {
            &mut self.__superclass
        }
    }
);

__inner_extern_class!(
    #[derive(Debug, PartialEq, Eq, Hash)]
    #[cfg(feature = "NSEnumerator")]
    pub struct NSEnumerator<ObjectType: ?Sized = AnyObject> {
        // SAFETY: Auto traits specified below.
        __superclass: UnsafeIgnoreAutoTraits<NSObject>,
        // Enumerators are basically the same as if we were storing
        // `NSMutableArray<ObjectType>`, and removed an element from that on
        // each iteration.
        //
        // This is only true because everything in this file has that type of
        // ownership; if something didn't, and instead had e.g. `Arc<T>`-like
        // ownership, we'd have to modify the ownership of enumerators to
        // match with that (if we wanted to safely use enumerators returned by
        // that type).
        __inner: PhantomData<Retained<ObjectType>>,
    }

    #[cfg(feature = "NSEnumerator")]
    unsafe impl<ObjectType: ?Sized + Message> ClassType for NSEnumerator<ObjectType> {
        type Super = NSObject;
        type Mutability = mutability::Mutable;

        fn as_super(&self) -> &Self::Super {
            &self.__superclass.0
        }

        fn as_super_mut(&mut self) -> &mut Self::Super {
            &mut self.__superclass.0
        }
    }
);