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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Utilities for the `NSDictionary` and `NSMutableDictionary` classes.
use alloc::vec::Vec;
#[cfg(feature = "NSObject")]
use core::cmp::min;
use core::fmt;
use core::hash::Hash;
use core::mem;
use core::ops::{Index, IndexMut};
use core::ptr::{self, NonNull};

#[cfg(feature = "NSObject")]
use objc2::mutability::IsRetainable;
use objc2::mutability::{CounterpartOrSelf, HasStableHash, IsIdCloneable, IsMutable};
use objc2::rc::Retained;
#[cfg(feature = "NSObject")]
use objc2::runtime::ProtocolObject;
#[cfg(feature = "NSObject")]
use objc2::ClassType;
use objc2::{extern_methods, Message};

#[cfg(feature = "NSEnumerator")]
use super::iter;
use super::util;
#[cfg(feature = "NSObject")]
use crate::Foundation::NSCopying;
use crate::Foundation::{NSDictionary, NSMutableDictionary};

#[cfg(feature = "NSObject")]
fn keys_to_ptr<Q>(keys: &[&Q]) -> *mut NonNull<ProtocolObject<dyn NSCopying>>
where
    Q: Message + NSCopying,
{
    let keys: *mut NonNull<Q> = util::ref_ptr_cast_const(keys.as_ptr());
    // SAFETY: `Q` is `Message + NSCopying`, and is therefore safe to cast to
    // `ProtocolObject<dyn NSCopying>`.
    let keys: *mut NonNull<ProtocolObject<dyn NSCopying>> = keys.cast();
    keys
}

#[cfg(feature = "NSObject")]
impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSDictionary<K, V> {
    pub fn from_vec<Q>(keys: &[&Q], mut objects: Vec<Retained<V>>) -> Retained<Self>
    where
        Q: Message + NSCopying + CounterpartOrSelf<Immutable = K>,
    {
        // Find the minimum of the two provided lengths, to ensure that we
        // don't read too far in one of the buffers.
        //
        // Note: We could also have chosen to just panic here if the buffers have
        // different lengths, either would be fine.
        let count = min(keys.len(), objects.len());

        let keys = keys_to_ptr(keys);
        let objects = util::retained_ptr_cast(objects.as_mut_ptr());

        // SAFETY:
        // - The objects are valid, similar reasoning as `NSArray::from_vec`.
        //
        // - The key must not be mutated, as that may cause the hash value to
        //   change, which is unsound as stated in:
        //   https://developer.apple.com/library/archive/documentation/General/Conceptual/CocoaEncyclopedia/ObjectMutability/ObjectMutability.html#//apple_ref/doc/uid/TP40010810-CH5-SW69
        //
        //   The dictionary always copies its keys, which is why we require
        //   `NSCopying` and use `CounterpartOrSelf` on all input data - we
        //   want to ensure that it is very clear that it's not actually
        //   `NSMutableString` that is being stored, but `NSString`.
        //
        //   But that is not by itself enough to verify that the key does not
        //   still contain interior mutable objects (e.g. if the copy was only
        //   a shallow copy), which is why we also require `HasStableHash`.
        //
        // - The length is lower than or equal to the length of the two arrays.
        unsafe { Self::initWithObjects_forKeys_count(Self::alloc(), objects, keys, count) }
    }

    pub fn from_id_slice<Q>(keys: &[&Q], objects: &[Retained<V>]) -> Retained<Self>
    where
        Q: Message + NSCopying + CounterpartOrSelf<Immutable = K>,
        V: IsIdCloneable,
    {
        let count = min(keys.len(), objects.len());

        let keys = keys_to_ptr(keys);
        let objects = util::retained_ptr_cast_const(objects.as_ptr());

        // SAFETY: See `NSDictionary::from_vec` and `NSArray::from_id_slice`.
        unsafe { Self::initWithObjects_forKeys_count(Self::alloc(), objects, keys, count) }
    }

    pub fn from_slice<Q>(keys: &[&Q], objects: &[&V]) -> Retained<Self>
    where
        Q: Message + NSCopying + CounterpartOrSelf<Immutable = K>,
        V: IsRetainable,
    {
        let count = min(keys.len(), objects.len());

        let keys = keys_to_ptr(keys);
        let objects = util::ref_ptr_cast_const(objects.as_ptr());

        // SAFETY: See `NSDictionary::from_vec` and `NSArray::from_slice`.
        unsafe { Self::initWithObjects_forKeys_count(Self::alloc(), objects, keys, count) }
    }
}

#[cfg(feature = "NSObject")]
impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSMutableDictionary<K, V> {
    pub fn from_vec<Q>(keys: &[&Q], mut objects: Vec<Retained<V>>) -> Retained<Self>
    where
        Q: Message + NSCopying + CounterpartOrSelf<Immutable = K>,
    {
        let count = min(keys.len(), objects.len());

        let keys: *mut NonNull<Q> = util::ref_ptr_cast_const(keys.as_ptr());
        let keys: *mut NonNull<ProtocolObject<dyn NSCopying>> = keys.cast();
        let objects = util::retained_ptr_cast(objects.as_mut_ptr());

        // SAFETY: See `NSDictionary::from_vec`
        unsafe { Self::initWithObjects_forKeys_count(Self::alloc(), objects, keys, count) }
    }

    pub fn from_id_slice<Q>(keys: &[&Q], objects: &[Retained<V>]) -> Retained<Self>
    where
        Q: Message + NSCopying + CounterpartOrSelf<Immutable = K>,
        V: IsIdCloneable,
    {
        let count = min(keys.len(), objects.len());

        let keys = keys_to_ptr(keys);
        let objects = util::retained_ptr_cast_const(objects.as_ptr());

        // SAFETY: See `NSDictionary::from_vec` and `NSArray::from_id_slice`.
        unsafe { Self::initWithObjects_forKeys_count(Self::alloc(), objects, keys, count) }
    }

    pub fn from_slice<Q>(keys: &[&Q], objects: &[&V]) -> Retained<Self>
    where
        Q: Message + NSCopying + CounterpartOrSelf<Immutable = K>,
        V: IsRetainable,
    {
        let count = min(keys.len(), objects.len());

        let keys = keys_to_ptr(keys);
        let objects = util::ref_ptr_cast_const(objects.as_ptr());

        // SAFETY: See `NSDictionary::from_vec` and `NSArray::from_slice`.
        unsafe { Self::initWithObjects_forKeys_count(Self::alloc(), objects, keys, count) }
    }
}

// Note: We'd like to make getter methods take `K: Borrow<Q>` like
// `std::collections::HashMap`, so that e.g.
// `NSDictionary<NSString, ...>` could take a `&NSObject` as input,
// and still make that work since `NSString` borrows to `NSObject`.
//
// But we can't really, at least not with extra `unsafe` / an extra
// trait, since we don't control how the comparisons happen.
//
// The most useful alternative would probably be to take
// `impl AsRef<K>`, but objc2 classes deref to their superclass anyhow, so
// let's just use a simple normal reference.

extern_methods!(
    unsafe impl<K: Message + Eq + Hash, V: Message> NSDictionary<K, V> {
        #[doc(alias = "objectForKey:")]
        #[method(objectForKey:)]
        pub fn get(&self, key: &K) -> Option<&V>;

        #[doc(alias = "objectForKey:")]
        pub fn get_retained(&self, key: &K) -> Option<Retained<V>>
        where
            V: IsIdCloneable,
        {
            // SAFETY: The object is stored in the dictionary
            self.get(key)
                .map(|obj| unsafe { util::collection_retain(obj) })
        }

        /// Returns a mutable reference to the value corresponding to the key.
        ///
        /// # Examples
        ///
        #[cfg_attr(all(feature = "NSString", feature = "NSObject"), doc = "```")]
        #[cfg_attr(
            not(all(feature = "NSString", feature = "NSObject")),
            doc = "```ignore"
        )]
        /// use objc2_foundation::{ns_string, NSMutableDictionary, NSMutableString, NSString};
        ///
        /// let mut dict = NSMutableDictionary::new();
        /// dict.insert_id(ns_string!("one"), NSMutableString::new());
        /// println!("{:?}", dict.get_mut(ns_string!("one")));
        /// ```
        #[doc(alias = "objectForKey:")]
        #[method(objectForKey:)]
        pub fn get_mut(&mut self, key: &K) -> Option<&mut V>
        where
            V: IsMutable;
    }
);

impl<K: Message, V: Message> NSDictionary<K, V> {
    pub fn len(&self) -> usize {
        self.count()
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[doc(alias = "getObjects:andKeys:")]
    pub fn keys_vec(&self) -> Vec<&K> {
        let len = self.len();
        let mut keys = Vec::with_capacity(len);
        unsafe {
            #[allow(deprecated)]
            self.getObjects_andKeys(ptr::null_mut(), keys.as_mut_ptr());
            keys.set_len(len);
            mem::transmute::<Vec<NonNull<K>>, Vec<&K>>(keys)
        }
    }

    // We don't provide `keys_mut_vec`, since keys are immutable.

    #[doc(alias = "getObjects:andKeys:")]
    pub fn values_vec(&self) -> Vec<&V> {
        let len = self.len();
        let mut vals = Vec::with_capacity(len);
        unsafe {
            #[allow(deprecated)]
            self.getObjects_andKeys(vals.as_mut_ptr(), ptr::null_mut());
            vals.set_len(len);
            mem::transmute::<Vec<NonNull<V>>, Vec<&V>>(vals)
        }
    }

    /// Returns a vector of mutable references to the values in the dictionary.
    ///
    /// # Examples
    ///
    #[cfg_attr(all(feature = "NSString", feature = "NSObject"), doc = "```")]
    #[cfg_attr(
        not(all(feature = "NSString", feature = "NSObject")),
        doc = "```ignore"
    )]
    /// use objc2_foundation::{ns_string, NSMutableDictionary, NSMutableString, NSString};
    ///
    /// let mut dict = NSMutableDictionary::new();
    /// dict.insert_id(ns_string!("one"), NSMutableString::from_str("two"));
    /// for val in dict.values_mut() {
    ///     println!("{:?}", val);
    /// }
    /// ```
    #[doc(alias = "getObjects:andKeys:")]
    pub fn values_vec_mut(&mut self) -> Vec<&mut V>
    where
        V: IsMutable,
    {
        let len = self.len();
        let mut vals = Vec::with_capacity(len);
        unsafe {
            #[allow(deprecated)]
            self.getObjects_andKeys(vals.as_mut_ptr(), ptr::null_mut());
            vals.set_len(len);
            mem::transmute::<Vec<NonNull<V>>, Vec<&mut V>>(vals)
        }
    }

    #[doc(alias = "getObjects:andKeys:")]
    pub fn to_vecs(&self) -> (Vec<&K>, Vec<&V>) {
        let len = self.len();
        let mut keys = Vec::with_capacity(len);
        let mut objs = Vec::with_capacity(len);
        unsafe {
            #[allow(deprecated)]
            self.getObjects_andKeys(objs.as_mut_ptr(), keys.as_mut_ptr());
            keys.set_len(len);
            objs.set_len(len);
            (
                mem::transmute::<Vec<NonNull<K>>, Vec<&K>>(keys),
                mem::transmute::<Vec<NonNull<V>>, Vec<&V>>(objs),
            )
        }
    }

    /// Returns an [`NSArray`] containing the dictionary's values.
    ///
    /// [`NSArray`]: crate::Foundation::NSArray
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// use objc2_foundation::{ns_string, NSMutableDictionary, NSObject, NSString};
    ///
    /// let mut dict = NSMutableDictionary::new();
    /// dict.insert_id(ns_string!("one"), NSObject::new());
    /// let array = dict.to_array();
    /// assert_eq!(array.len(), 1);
    /// ```
    #[cfg(feature = "NSArray")]
    pub fn to_array(&self) -> Retained<crate::Foundation::NSArray<V>>
    where
        V: IsIdCloneable,
    {
        // SAFETY: The elements are retainable behind `Retained<V>`, so getting
        // another reference to them (via. `NSArray`) is sound.
        unsafe { self.allValues() }
    }
}

impl<K: Message + Eq + Hash + HasStableHash, V: Message> NSMutableDictionary<K, V> {
    /// Inserts a key-value pair into the dictionary.
    ///
    /// If the dictionary did not have this key present, None is returned.
    /// If the dictionary did have this key present, the value is updated,
    /// and the old value is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use objc2_foundation::{NSMutableDictionary, NSObject, ns_string};
    ///
    /// let mut dict = NSMutableDictionary::new();
    /// dict.insert_id(ns_string!("one"), NSObject::new());
    /// ```
    #[cfg(feature = "NSObject")]
    #[doc(alias = "setObject:forKey:")]
    pub fn insert_id(&mut self, key: &K, value: Retained<V>) -> Option<Retained<V>>
    where
        K: NSCopying + CounterpartOrSelf<Immutable = K>,
    {
        // SAFETY: We remove the object from the dictionary below
        let old_obj = self
            .get(key)
            .map(|old_obj| unsafe { util::mutable_collection_retain_removed(old_obj) });

        let key = ProtocolObject::from_ref(key);
        // SAFETY: We have ownership over the value.
        unsafe { self.setObject_forKey(&value, key) };
        old_obj
    }

    /// Inserts a key-value pair into the dictionary.
    ///
    /// If the dictionary did not have this key present, None is returned.
    /// If the dictionary did have this key present, the value is updated,
    /// and the old value is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use objc2_foundation::{ns_string, NSCopying, NSMutableDictionary};
    ///
    /// let mut dict = NSMutableDictionary::new();
    /// dict.insert_id(ns_string!("key"), ns_string!("value").copy());
    /// ```
    #[cfg(feature = "NSObject")]
    #[doc(alias = "setObject:forKey:")]
    pub fn insert(&mut self, key: &K, value: &V) -> Option<Retained<V>>
    where
        K: NSCopying + CounterpartOrSelf<Immutable = K>,
        V: IsRetainable,
    {
        // SAFETY: We remove the object from the dictionary below
        let old_obj = self
            .get(key)
            .map(|old_obj| unsafe { util::mutable_collection_retain_removed(old_obj) });

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

    /// Removes a key from the dictionary, returning the value at the key
    /// if the key was previously in the dictionary.
    ///
    /// # Examples
    ///
    #[cfg_attr(all(feature = "NSString", feature = "NSObject"), doc = "```")]
    #[cfg_attr(
        not(all(feature = "NSString", feature = "NSObject")),
        doc = "```ignore"
    )]
    /// use objc2_foundation::{ns_string, NSMutableDictionary, NSObject};
    ///
    /// let mut dict = NSMutableDictionary::new();
    /// dict.insert_id(ns_string!("one"), NSObject::new());
    /// dict.remove(ns_string!("one"));
    /// assert!(dict.is_empty());
    /// ```
    #[doc(alias = "removeObjectForKey:")]
    pub fn remove(&mut self, key: &K) -> Option<Retained<V>>
    where
        K: CounterpartOrSelf<Immutable = K>,
    {
        // SAFETY: We remove the object from the dictionary below
        let old_obj = self
            .get(key)
            .map(|old_obj| unsafe { util::mutable_collection_retain_removed(old_obj) });
        self.removeObjectForKey(key);
        old_obj
    }
}

impl<K: Message, V: Message> NSDictionary<K, V> {
    #[doc(alias = "keyEnumerator")]
    #[cfg(feature = "NSEnumerator")]
    pub fn keys(&self) -> Keys<'_, K, V> {
        Keys(iter::Iter::new(self))
    }

    #[doc(alias = "keyEnumerator")]
    #[cfg(feature = "NSEnumerator")]
    pub fn keys_retained(&self) -> KeysRetained<'_, K, V>
    where
        K: IsIdCloneable,
    {
        KeysRetained(iter::IterRetained::new(self))
    }

    // TODO: Is this ever useful?
    // pub fn into_keys(this: Retained<Self>) -> IntoKeys<K, V> {
    //     todo!()
    // }

    #[doc(alias = "objectEnumerator")]
    #[cfg(feature = "NSEnumerator")]
    pub fn values(&self) -> Values<'_, K, V> {
        let enumerator = unsafe { self.objectEnumerator() };
        // SAFETY: The enumerator came from the dictionary.
        Values(unsafe { iter::IterWithBackingEnum::new(self, enumerator) })
    }

    #[doc(alias = "objectEnumerator")]
    #[cfg(feature = "NSEnumerator")]
    pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
    where
        V: IsMutable,
    {
        let enumerator = unsafe { self.objectEnumerator() };
        // SAFETY: The enumerator came from the dictionary.
        ValuesMut(unsafe { iter::IterMutWithBackingEnum::new(self, enumerator) })
    }

    #[doc(alias = "objectEnumerator")]
    #[cfg(feature = "NSEnumerator")]
    pub fn values_retained(&self) -> ValuesRetained<'_, K, V>
    where
        V: IsIdCloneable,
    {
        let enumerator = unsafe { self.objectEnumerator() };
        // SAFETY: The enumerator came from the dictionary.
        ValuesRetained(unsafe { iter::IterRetainedWithBackingEnum::new(self, enumerator) })
    }

    #[doc(alias = "objectEnumerator")]
    #[cfg(feature = "NSEnumerator")]
    pub fn into_values(this: Retained<Self>) -> IntoValues<K, V>
    where
        V: IsIdCloneable,
    {
        let enumerator = unsafe { this.objectEnumerator() };
        // SAFETY: The enumerator came from the dictionary.
        IntoValues(unsafe { iter::IntoIterWithBackingEnum::new_immutable(this, enumerator) })
    }
}

#[cfg(feature = "NSEnumerator")]
unsafe impl<K: Message, V: Message> iter::FastEnumerationHelper for NSDictionary<K, V> {
    // Fast enumeration for dictionaries returns the keys.
    type Item = K;

    #[inline]
    fn maybe_len(&self) -> Option<usize> {
        Some(self.len())
    }
}

#[cfg(feature = "NSEnumerator")]
unsafe impl<K: Message, V: Message> iter::FastEnumerationHelper for NSMutableDictionary<K, V> {
    // The same goes for mutable dictionaries.
    type Item = K;

    #[inline]
    fn maybe_len(&self) -> Option<usize> {
        Some(self.len())
    }
}

// We also cfg-gate `Keys` behind `NSEnumerator` for symmetry, even though we
// don't necessarily need to.
#[cfg(feature = "NSEnumerator")]
mod iter_helpers {
    use super::*;
    use crate::Foundation::NSEnumerator;

    /// An iterator over the keys of a `NSDictionary`.
    #[derive(Debug)]
    pub struct Keys<'a, K: Message, V: Message>(pub(super) iter::Iter<'a, NSDictionary<K, V>>);

    __impl_iter! {
        impl<'a, K: Message, V: Message> Iterator<Item = &'a K> for Keys<'a, K, V> { ... }
    }

    /// An iterator that retains the keys of a `NSDictionary`.
    #[derive(Debug)]
    pub struct KeysRetained<'a, K: Message, V: Message>(
        pub(super) iter::IterRetained<'a, NSDictionary<K, V>>,
    );

    __impl_iter! {
        impl<'a, K: Message + IsIdCloneable, V: Message> Iterator<Item = Retained<K>> for KeysRetained<'a, K, V> { ... }
    }

    /// An iterator over the values of a `NSDictionary`.
    #[derive(Debug)]
    pub struct Values<'a, K: Message, V: Message>(
        pub(super) iter::IterWithBackingEnum<'a, NSDictionary<K, V>, NSEnumerator<V>>,
    );

    __impl_iter! {
        impl<'a, K: Message, V: Message> Iterator<Item = &'a V> for Values<'a, K, V> { ... }
    }

    /// A mutable iterator over the values of a `NSDictionary`.
    #[derive(Debug)]
    pub struct ValuesMut<'a, K: Message, V: Message>(
        pub(super) iter::IterMutWithBackingEnum<'a, NSDictionary<K, V>, NSEnumerator<V>>,
    );

    __impl_iter! {
        impl<'a, K: Message, V: Message + IsMutable> Iterator<Item = &'a mut V> for ValuesMut<'a, K, V> { ... }
    }

    /// A iterator that retains the values of a `NSDictionary`.
    #[derive(Debug)]
    pub struct ValuesRetained<'a, K: Message, V: Message>(
        pub(super) iter::IterRetainedWithBackingEnum<'a, NSDictionary<K, V>, NSEnumerator<V>>,
    );

    __impl_iter! {
        impl<'a, K: Message, V: Message + IsIdCloneable> Iterator<Item = Retained<V>> for ValuesRetained<'a, K, V> { ... }
    }

    /// A consuming iterator over the values of a `NSDictionary`.
    #[derive(Debug)]
    pub struct IntoValues<K: Message, V: Message>(
        pub(super) iter::IntoIterWithBackingEnum<NSDictionary<K, V>, NSEnumerator<V>>,
    );

    __impl_iter! {
        impl<K: Message, V: Message> Iterator<Item = Retained<V>> for IntoValues<K, V> { ... }
    }
}

#[cfg(feature = "NSEnumerator")]
pub use self::iter_helpers::*;

impl<'a, K: Message + Eq + Hash, V: Message> Index<&'a K> for NSDictionary<K, V> {
    type Output = V;

    fn index<'s>(&'s self, index: &'a K) -> &'s V {
        self.get(index).unwrap()
    }
}

impl<'a, K: Message + Eq + Hash, V: Message> Index<&'a K> for NSMutableDictionary<K, V> {
    type Output = V;

    fn index<'s>(&'s self, index: &'a K) -> &'s V {
        self.get(index).unwrap()
    }
}

impl<'a, K: Message + Eq + Hash, V: Message + IsMutable> IndexMut<&'a K> for NSDictionary<K, V> {
    fn index_mut<'s>(&'s mut self, index: &'a K) -> &'s mut V {
        self.get_mut(index).unwrap()
    }
}

impl<'a, K: Message + Eq + Hash, V: Message + IsMutable> IndexMut<&'a K>
    for NSMutableDictionary<K, V>
{
    fn index_mut<'s>(&'s mut self, index: &'a K) -> &'s mut V {
        self.get_mut(index).unwrap()
    }
}

impl<K: fmt::Debug + Message, V: fmt::Debug + Message> fmt::Debug for NSDictionary<K, V> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (keys, values) = self.to_vecs();
        let iter = keys.into_iter().zip(values);
        f.debug_map().entries(iter).finish()
    }
}

impl<K: fmt::Debug + Message, V: fmt::Debug + Message> fmt::Debug for NSMutableDictionary<K, V> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Debug::fmt(&**self, f)
    }
}