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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
use std::any::Any;
use std::hash::BuildHasher;
use std::marker::PhantomData;
use std::ops::Deref;
use std::{fmt::Debug, ops::DerefMut};

use dashmap::DashMap;

use crate::dashentry;
use crate::typedkey::{Key, SyncTypedKey, TypedKeyRef};
use crate::typedvalue::SyncTypedMapValue;
use crate::TypedMapKey;

use std::collections::hash_map::RandomState;

/// A concurrent hash map that can store keys of any type that implements [`TypedMapKey`] and values of
/// type defined by [`TypedMapKey::Value`]. One can use Marker to define multiple "key-value" type
/// mappings. Under the hood the [`DashMap`] is used. Note: that it will deadlock whenever DashMap will.
///
/// # Examples
/// ```
/// use std::sync::Arc;
/// use typedmap::TypedDashMap;
/// use typedmap::TypedMapKey;
///
/// struct Configs;
/// struct Services;
///
/// #[derive(Hash, PartialEq, Eq)]
/// struct ServiceA(usize);
///
/// // Implement key-value mapping for Configs marker
/// impl TypedMapKey<Configs> for ServiceA {
///     type Value = usize;
/// }
///
/// // Implement key-value mapping for Services marker
/// impl TypedMapKey<Services> for ServiceA {
///     type Value = &'static str;
/// }
///
/// #[derive(Hash, PartialEq, Eq)]
/// struct ServiceB(&'static str);
///
/// // Implement key-value mapping for Configs marker
/// impl TypedMapKey<Configs> for ServiceB {
///     type Value = Vec<&'static str>;
/// }
///
/// // Implement key-value mapping for Services marker
/// impl TypedMapKey<Services> for ServiceB {
///     type Value = usize;
/// }
///
/// // Implement key-value mapping for default (i.e. ()) marker
/// impl TypedMapKey for ServiceB {
///     type Value = String;
/// }
///
/// let configs: Arc<TypedDashMap<Configs>> = Arc::new(TypedDashMap::new());
/// let services: Arc<TypedDashMap<Services>> = Arc::new(TypedDashMap::new());
/// let default: Arc<TypedDashMap> = Arc::new(TypedDashMap::new());
///
/// let configs1 = Arc::clone(&configs);
/// let services1 = Arc::clone(&services);
/// let t1 = std::thread::spawn(move ||{
///     configs1.insert(ServiceA(0), 1);
///     services1.insert(ServiceA(0), "one");
/// });
/// // Line below would not compile, because TypeMapKey<Marker=()>
/// // is not implemented for Key.
/// // default.insert(Key(0), 1);
///
/// // Line below would not compile, because SerivceA key defines
/// // type value as usize for Configs marker (not &'static str)
/// // configs.insert(ServiceA(0), "one");
///
/// let configs2 = Arc::clone(&configs);
/// let services2 = Arc::clone(&services);
/// let default2 = Arc::clone(&default);
/// let t2 = std::thread::spawn(move || {
///     configs2.insert(ServiceB("zero"), vec!["one"]);
///     services2.insert(ServiceB("zero"), 32);
///     default2.insert(ServiceB("zero"), "one".to_owned());
/// });
///
/// t1.join().unwrap();
/// t2.join().unwrap();
///
/// assert_eq!(*configs.get(&ServiceB("zero")).unwrap(), vec!["one"]);
/// assert_eq!(*services.get(&ServiceB("zero")).unwrap(), 32);
/// assert_eq!(*default.get(&ServiceB("zero")).unwrap(), "one".to_owned());
///
/// ```
pub struct TypedDashMap<Marker = (), S = RandomState> {
    state: DashMap<SyncTypedKey, SyncTypedMapValue, S>,
    _phantom: std::marker::PhantomData<Marker>,
}

const INVALID_KEY: &str = "Broken TypedDashMap: invalid key type";
const INVALID_VALUE: &str = "Broken TypedDashMap: invalid value type";

impl<Marker> TypedDashMap<Marker> {
    /// Creates a new TypedDashMap with specified marker type.
    ///
    /// # Examples:
    /// ```rust
    /// use typedmap::TypedMap;
    ///
    /// struct Configs;
    /// let map = TypedMap::<Configs>::new();
    /// ```
    pub fn new() -> Self {
        TypedDashMap {
            state: Default::default(),
            _phantom: PhantomData,
        }
    }

    /// Creates a new TypedDashMap with a specified capacity and specified marker type
    pub fn with_capacity(capacity: usize) -> Self {
        TypedDashMap {
            state: DashMap::with_capacity(capacity),
            _phantom: PhantomData,
        }
    }
}

impl<Marker, S> TypedDashMap<Marker, S>
where
    S: 'static + BuildHasher + Clone,
{
    /// Creates a new TypedDashMap with specified capacity, hasher and marker type.
    pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
        TypedDashMap {
            state: DashMap::with_capacity_and_hasher(capacity, hash_builder),
            _phantom: PhantomData,
        }
    }

    /// Creates a new TypedDashMap with specified hasher and marker type.
    pub fn with_hasher(hash_builder: S) -> Self {
        TypedDashMap {
            state: DashMap::with_hasher(hash_builder),
            _phantom: PhantomData,
        }
    }

    /// Inserts a key and a value into the map.
    ///
    /// If the map did not have this key present, None is returned.
    ///
    /// If the map did have this key present, the value is updated, and old value is returned.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Debug, Hash, PartialEq, Eq)]
    /// struct Key(usize);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = usize;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
    /// assert!(map.insert(Key(3), 4).is_none());
    /// assert_eq!(map.insert(Key(3), 5), Some(4));
    /// assert_eq!(*map.get(&Key(3)).unwrap(), 5);
    /// ```
    pub fn insert<K: 'static + TypedMapKey<Marker> + Send + Sync>(
        &self,
        key: K,
        value: K::Value,
    ) -> Option<K::Value>
    where
        K::Value: Send + Sync,
    {
        let typed_key = SyncTypedKey::from_key(key);
        let value = SyncTypedMapValue::from_value(value);
        let old_value = self.state.insert(typed_key, value)?;

        Some(old_value.downcast::<K::Value>().expect(INVALID_VALUE))
    }

    /// Get the entry of a key if it exists in the map.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Hash, PartialEq, Eq)]
    /// struct Key(usize);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = usize;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
    /// assert!(map.get(&Key(3)).is_none());
    /// map.insert(Key(3), 4);
    /// assert_eq!(*map.get(&Key(3)).unwrap(), 4);
    /// ```
    pub fn get<K: 'static + TypedMapKey<Marker> + Send + Sync>(
        &self,
        key: &K,
    ) -> Option<Ref<'_, Marker, K, S>>
    where
        K::Value: Send + Sync,
    {
        let typed_key = TypedKeyRef::from_key_ref(key);
        let value = self.state.get(&typed_key as &dyn Key)?;
        Some(Ref(
            value,
            std::marker::PhantomData,
            std::marker::PhantomData,
        ))
    }
    /// Get mutable entry of a key if it exists in the map.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Hash, PartialEq, Eq)]
    /// struct Key(usize);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = usize;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
    /// assert!(map.get_mut(&Key(3)).is_none());
    /// map.insert(Key(3), 4);
    /// *map.get_mut(&Key(3)).unwrap() = 5;
    /// assert_eq!(*map.get(&Key(3)).unwrap().value(), 5);
    /// ```
    pub fn get_mut<K: 'static + TypedMapKey<Marker> + Send + Sync>(
        &self,
        key: &K,
    ) -> Option<RefMut<'_, Marker, K, S>>
    where
        K::Value: Send + Sync,
    {
        let typed_key = TypedKeyRef::from_key_ref(key);
        let value = self.state.get_mut(&typed_key as &dyn Key)?;
        Some(RefMut(
            value,
            std::marker::PhantomData,
            std::marker::PhantomData,
        ))
    }

    /// Check if the map contains a specific key.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Hash, PartialEq, Eq)]
    /// struct Key(usize);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = usize;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
    /// assert!(!map.contains_key(&Key(3)));
    /// map.insert(Key(3), 4);
    /// assert!(map.contains_key(&Key(3)));
    /// ```
    pub fn contains_key<K: 'static + TypedMapKey<Marker> + Send + Sync>(&self, key: &K) -> bool
    where
        K::Value: Send + Sync,
    {
        let typed_key = TypedKeyRef::from_key_ref(key);
        self.state.contains_key(&typed_key as &dyn Key)
    }

    /// Removes an entry from the map.
    ///
    /// Returns both key and value if the key existed and the entry was removed. Otherwise returns None.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Debug, Hash, PartialEq, Eq)]
    /// struct Key(usize);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = usize;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
    /// assert!(map.remove(&Key(3)).is_none());
    /// map.insert(Key(3), 4);
    /// assert!(map.contains_key(&Key(3)));
    /// assert_eq!(map.remove(&Key(3)), Some((Key(3), 4)));
    /// assert!(!map.contains_key(&Key(3)));
    /// ```
    pub fn remove<K: 'static + TypedMapKey<Marker> + Send + Sync>(
        &self,
        key: &K,
    ) -> Option<(K, K::Value)>
    where
        K::Value: Send + Sync,
    {
        let typed_key = TypedKeyRef::from_key_ref(key);
        let (key, value) = self.state.remove(&typed_key as &dyn Key)?;
        let key = key.downcast().ok().expect(INVALID_KEY);
        let value = value.downcast().expect(INVALID_VALUE);
        Some((key, value))
    }

    /// Removes an entry from the map the provided conditional function returned true.
    ///
    /// Returns both key and value if the key existed and the entry was removed. Otherwise returns None.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Debug, Hash, PartialEq, Eq)]
    /// struct Key(usize);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = usize;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
    /// assert!(map.remove(&Key(3)).is_none());
    /// map.insert(Key(3), 4);
    /// assert!(map.contains_key(&Key(3)));
    /// assert_eq!(map.remove_if(&Key(3), |k, v| false), None);
    /// assert!(map.contains_key(&Key(3)));
    /// assert_eq!(map.remove_if(&Key(3), |k, v| true), Some((Key(3), 4)));
    /// assert!(!map.contains_key(&Key(3)));
    /// ```
    pub fn remove_if<K: 'static + TypedMapKey<Marker> + Send + Sync>(
        &self,
        key: &K,
        f: impl FnOnce(&K, &K::Value) -> bool,
    ) -> Option<(K, K::Value)>
    where
        K::Value: Send + Sync,
    {
        let typed_key = TypedKeyRef::from_key_ref(key);
        let f = move |typed_key: &SyncTypedKey, typed_value: &SyncTypedMapValue| {
            let k = typed_key.downcast_ref().expect(INVALID_KEY);
            let v = typed_value.downcast_ref().expect(INVALID_VALUE);
            f(k, v)
        };
        let (key, value) = self.state.remove_if(&typed_key as &dyn Key, f)?;
        let key = key.downcast().ok().expect(INVALID_KEY);
        let value = value.downcast().expect(INVALID_VALUE);
        Some((key, value))
    }

    /// Get the amount of entries in the map.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Hash, PartialEq, Eq, Debug)]
    /// struct Key(usize);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = usize;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
    /// assert_eq!(map.len(), 0);
    /// map.insert(Key(3), 4);
    /// assert_eq!(map.len(), 1);
    /// ```
    pub fn len(&self) -> usize {
        self.state.len()
    }

    /// Returns true if the map contains no elements.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Hash, PartialEq, Eq)]
    /// struct Key(usize);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = f32;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::with_capacity(10);
    /// assert!(map.is_empty());
    /// map.insert(Key(3), 4.0);
    /// assert!(!map.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.state.is_empty()
    }

    /// Clears the map, removing all key-value pairs.
    ///
    /// # Examples:
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Hash, PartialEq, Eq)]
    /// struct Key(usize);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = f32;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::new();
    /// map.insert(Key(3), 4.0);
    /// map.clear();
    /// assert!(map.get(&Key(3)).is_none())
    /// // assert!(map.is_empty()); // for some reason this fails
    /// ```
    pub fn clear(&self) {
        self.state.clear();
    }

    /// An iterator visiting all key-value pairs in arbitrary order.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Hash, PartialEq, Eq, Debug)]
    /// struct Key(usize);
    /// #[derive(Hash, PartialEq, Eq, Debug)]
    /// struct SKey(&'static str);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = u32;
    /// }
    ///
    /// impl TypedMapKey for SKey {
    ///     type Value = usize;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::new();
    /// map.insert(Key(3), 3);
    /// map.insert(SKey("four"), 4);
    ///
    /// for key_value in map.iter() {
    ///     if let Some((key, value)) = key_value.downcast_pair_ref::<Key>() {
    ///         assert_eq!(key, &Key(3));
    ///         assert_eq!(value, &3u32);
    ///     }
    ///
    ///     if let Some((key, value)) = key_value.downcast_pair_ref::<SKey>() {
    ///         assert_eq!(key, &SKey("four"));
    ///         assert_eq!(value, &4);
    ///     }
    /// }
    /// ```
    pub fn iter(&self) -> Iter<'_, Marker, S> {
        Iter(self.state.iter(), PhantomData)
    }

    /// Gets the given key's corresponding entry in the map for in-place manipulation.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Hash, PartialEq, Eq)]
    /// struct Key(char);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = usize;
    /// }
    ///
    /// let mut letters: TypedDashMap = TypedDashMap::new();
    /// for ch in "a short treatise on fungi".chars() {
    ///    let mut counter = letters.entry(Key(ch)).or_insert(0);
    ///    *counter += 1;
    /// }
    /// assert_eq!(letters.get(&Key('s')).unwrap().value(), &2);
    /// assert_eq!(letters.get(&Key('t')).unwrap().value(), &3);
    /// assert_eq!(letters.get(&Key('u')).unwrap().value(), &1);
    /// assert!(letters.get(&Key('y')).is_none());
    /// ```
    pub fn entry<K: 'static + TypedMapKey<Marker> + Send + Sync>(
        &mut self,
        key: K,
    ) -> dashentry::Entry<'_, K, Marker, S>
    where
        K::Value: Send + Sync,
    {
        let typed_key = SyncTypedKey::from_key(key);
        dashentry::map_entry(self.state.entry(typed_key))
    }

    /// Retain elements that the filter closure returns true for.
    ///
    /// # Examples
    /// ```
    /// use typedmap::TypedDashMap;
    /// use typedmap::TypedMapKey;
    ///
    /// #[derive(Hash, PartialEq, Eq, Debug)]
    /// struct Key(usize);
    /// #[derive(Hash, PartialEq, Eq, Debug)]
    /// struct SKey(&'static str);
    ///
    /// impl TypedMapKey for Key {
    ///     type Value = u32;
    /// }
    ///
    /// impl TypedMapKey for SKey {
    ///     type Value = usize;
    /// }
    ///
    /// let mut map: TypedDashMap = TypedDashMap::new();
    /// map.insert(Key(3), 3);
    /// map.insert(SKey("four"), 4);
    ///
    /// map.retain(|kv| kv.downcast_key_ref::<Key>().is_some());
    /// assert!(map.contains_key(&Key(3)));
    /// ```
    pub fn retain(&self, mut predicate: impl FnMut(TypedKeyValueRef<Marker>) -> bool) {
        let ff = move |key: &SyncTypedKey, value: &mut SyncTypedMapValue| {
            let kv = TypedKeyValueRef {
                key,
                value,
                _marker: PhantomData,
            };
            predicate(kv)
        };
        self.state.retain(ff);
    }
}

impl<Marker> Default for TypedDashMap<Marker> {
    fn default() -> Self {
        TypedDashMap::new()
    }
}

/// An iterator over the entries of a TypedDashMap
///
/// This `struct` is created by ['iter'] method on [`TypedDashMap`]. See its documentation for more.
///
/// ['iter']: crate::TypedMap::iter
///
/// # Example
/// ```
/// use typedmap::TypedDashMap;
/// use typedmap::TypedMapKey;
///
/// #[derive(Hash, PartialEq, Eq, Debug)]
/// struct Key(usize);
///
/// impl TypedMapKey for Key {
///     type Value = u32;
/// }
///
/// let mut map: TypedDashMap = TypedDashMap::new();
/// map.insert(Key(3), 3);
/// let iter = map.iter();
///
pub struct Iter<'a, Marker, S>(
    dashmap::iter::Iter<'a, SyncTypedKey, SyncTypedMapValue, S>,
    PhantomData<Marker>,
);

impl<'a, Marker, S: BuildHasher + Clone> Iterator for Iter<'a, Marker, S> {
    type Item = TypedKeyValueGuard<'a, Marker, S>;

    fn next(&mut self) -> Option<Self::Item> {
        let key_value = self.0.next()?;
        Some(TypedKeyValueGuard {
            key_value,
            _marker: PhantomData,
        })
    }
}

pub struct TypedKeyValueGuard<'a, Marker, S> {
    key_value: dashmap::mapref::multiple::RefMulti<'a, SyncTypedKey, SyncTypedMapValue, S>,
    _marker: PhantomData<Marker>,
}

impl<'a, Marker, S: BuildHasher> TypedKeyValueGuard<'a, Marker, S> {
    /// Downcast key to reference of `K`. Returns `None` if not possible.
    pub fn downcast_key_ref<K: 'static + TypedMapKey<Marker>>(&self) -> Option<&'_ K> {
        self.key_value.key().downcast_ref()
    }

    /// Downcast value and returns reference or `None` if downcast failed.
    pub fn downcast_value_ref<V: 'static + Any>(&self) -> Option<&'_ V> {
        self.key_value.value().downcast_ref()
    }

    /// Downcast key to reference of `K` and value to reference of `K::Value`.
    /// Returns `None` if not possible.
    pub fn downcast_pair_ref<K: 'static + TypedMapKey<Marker>>(
        &self,
    ) -> Option<(&'_ K, &'_ K::Value)> {
        let (key, value) = self.key_value.pair();
        Some((key.downcast_ref()?, value.downcast_ref()?))
    }
}

/// An immutable reference
pub struct Ref<'a, Marker, K: 'static + TypedMapKey<Marker>, S>(
    dashmap::mapref::one::Ref<'a, SyncTypedKey, SyncTypedMapValue, S>,
    std::marker::PhantomData<K>,
    std::marker::PhantomData<Marker>,
);

impl<'a, Marker, K: 'static + TypedMapKey<Marker>, S: BuildHasher> Ref<'a, Marker, K, S> {
    pub fn key(&self) -> &K {
        self.0.key().downcast_ref::<K>().expect(INVALID_KEY)
    }

    pub fn value(&self) -> &K::Value {
        self.0
            .value()
            .downcast_ref::<K::Value>()
            .expect(INVALID_VALUE)
    }

    pub fn pair(&self) -> (&K, &K::Value) {
        (self.key(), self.value())
    }
}

impl<'a, Marker, K: TypedMapKey<Marker>, S: BuildHasher> Deref for Ref<'a, Marker, K, S> {
    type Target = K::Value;

    fn deref(&self) -> &K::Value {
        self.value()
    }
}

impl<'a, Marker, K: TypedMapKey<Marker>, S> Debug for Ref<'a, Marker, K, S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        f.write_str("Ref")
    }
}

/// A mutable reference
pub struct RefMut<'a, Marker, K: 'static + TypedMapKey<Marker>, S>(
    pub(crate) dashmap::mapref::one::RefMut<'a, SyncTypedKey, SyncTypedMapValue, S>,
    pub(crate) std::marker::PhantomData<K>,
    pub(crate) std::marker::PhantomData<Marker>,
);

impl<'a, Marker, K: 'static + TypedMapKey<Marker>, S: BuildHasher> RefMut<'a, Marker, K, S> {
    pub fn key(&self) -> &K {
        self.0.key().downcast_ref::<K>().expect(INVALID_KEY)
    }

    pub fn value(&self) -> &K::Value {
        self.0
            .value()
            .downcast_ref::<K::Value>()
            .expect(INVALID_VALUE)
    }

    pub fn value_mut(&mut self) -> &mut K::Value {
        self.0
            .value_mut()
            .downcast_mut::<K::Value>()
            .expect(INVALID_VALUE)
    }

    pub fn pair(&self) -> (&K, &K::Value) {
        (self.key(), self.value())
    }

    pub fn pair_mut(&mut self) -> (&K, &K::Value) {
        let (key, value) = self.0.pair_mut();
        let key = key.downcast_ref::<K>().expect(INVALID_KEY);
        let value = value.downcast_mut::<K::Value>().expect(INVALID_VALUE);
        (key, value)
    }
}

impl<'a, Marker, K: TypedMapKey<Marker>, S: BuildHasher> Deref for RefMut<'a, Marker, K, S> {
    type Target = K::Value;

    fn deref(&self) -> &K::Value {
        self.value()
    }
}

impl<'a, Marker, K: TypedMapKey<Marker>, S: BuildHasher> DerefMut for RefMut<'a, Marker, K, S> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.value_mut()
    }
}

impl<'a, Marker, K: TypedMapKey<Marker>, S> Debug for RefMut<'a, Marker, K, S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        f.write_str("RefMut")
    }
}

/// Represents borrowed pair of key and value.
pub struct TypedKeyValueRef<'a, Marker> {
    key: &'a SyncTypedKey,
    value: &'a SyncTypedMapValue,
    _marker: PhantomData<Marker>,
}

impl<'a, Marker> TypedKeyValueRef<'a, Marker> {
    /// Downcast key to reference of `K`. Returns `None` if not possible.
    pub fn downcast_key_ref<K: 'static + TypedMapKey<Marker>>(&self) -> Option<&'a K> {
        self.key.downcast_ref()
    }

    /// Downcast value and returns reference or `None` if downcast failed.
    pub fn downcast_value_ref<V: 'static + Any>(&self) -> Option<&'a V> {
        self.value.downcast_ref()
    }

    /// Downcast key to reference of `K` and value to reference of `K::Value`.
    /// Returns `None` if not possible.
    pub fn downcast_pair_ref<K: 'static + TypedMapKey<Marker>>(
        &self,
    ) -> Option<(&'a K, &'a K::Value)> {
        self.downcast_key_ref()
            .and_then(move |key| self.downcast_value_ref().map(move |value| (key, value)))
    }
}

#[cfg(test)]
mod tests {
    use crate::TypedDashMap;
    use crate::TypedMapKey;
    use std::hash::Hash;
    use std::sync::Arc;

    #[test]
    fn test_threads() {
        let map: Arc<TypedDashMap> = Arc::new(TypedDashMap::new());

        #[derive(Debug, Hash, PartialEq, Eq)]
        struct Key(String);

        impl TypedMapKey for Key {
            type Value = String;
        }

        let map1 = map.clone();
        let th1 = std::thread::spawn(move || {
            map1.insert(Key("k1".to_owned()), "v1".to_owned());
        });

        let map2 = map.clone();
        let th2 = std::thread::spawn(move || {
            map2.insert(Key("k2".to_owned()), "v2".to_owned());
        });

        th1.join().unwrap();
        th2.join().unwrap();

        let k1 = Key("k1".to_owned());
        let k2 = Key("k2".to_owned());

        let r = map.get(&k1).unwrap();
        assert_eq!(r.key(), &k1);
        assert_eq!(r.value(), &"v1".to_owned());

        let r = map.get(&k2).unwrap();
        assert_eq!(r.pair(), (&k2, &"v2".to_owned()));
    }
}