papaya 0.2.4

A fast and ergonomic concurrent hash-table for read-heavy workloads.
Documentation
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
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
use crate::raw::utils::MapGuard;
use crate::raw::{self, InsertResult};
use crate::Equivalent;
use seize::{Collector, Guard, LocalGuard, OwnedGuard};

use std::collections::hash_map::RandomState;
use std::fmt;
use std::hash::{BuildHasher, Hash};
use std::marker::PhantomData;

/// A concurrent hash table.
///
/// Most hash table operations require a [`Guard`](crate::Guard), which can be acquired through
/// [`HashMap::guard`] or using the [`HashMap::pin`] API. See the [crate-level documentation](crate#usage)
/// for details.
pub struct HashMap<K, V, S = RandomState> {
    raw: raw::HashMap<K, V, S>,
}

// Safety: `HashMap` acts as a single-threaded collection on a single thread.
// References to keys and values cannot outlive the map's lifetime on a given
// thread.
unsafe impl<K: Send, V: Send, S: Send> Send for HashMap<K, V, S> {}

// Safety: We only ever hand out `&{K, V}` through shared references to the map,
// and never `&mut {K, V}` except through synchronized memory reclamation.
//
// However, we require `{K, V}: Send` as keys and values may be removed and dropped
// on a different thread than they were created on through shared access to the
// `HashMap`.
//
// Additionally, `HashMap` owns its `seize::Collector` and never exposes it,
// so multiple threads cannot be involved in reclamation without sharing the
// `HashMap` itself. If this was not true, we would require stricter bounds
// on `HashMap` operations themselves.
unsafe impl<K: Send + Sync, V: Send + Sync, S: Sync> Sync for HashMap<K, V, S> {}

/// A builder for a [`HashMap`].
///
/// # Examples
///
/// ```rust
/// use papaya::{HashMap, ResizeMode};
/// use seize::Collector;
/// use std::collections::hash_map::RandomState;
///
/// let map: HashMap<i32, i32> = HashMap::builder()
///     // Set the initial capacity.
///     .capacity(2048)
///     // Set the hasher.
///     .hasher(RandomState::new())
///     // Set the resize mode.
///     .resize_mode(ResizeMode::Blocking)
///     // Set a custom garbage collector.
///     .collector(Collector::new().batch_size(128))
///     // Construct the hash map.
///     .build();
/// ```
pub struct HashMapBuilder<K, V, S = RandomState> {
    hasher: S,
    capacity: usize,
    collector: Collector,
    resize_mode: ResizeMode,
    _kv: PhantomData<(K, V)>,
}

impl<K, V> HashMapBuilder<K, V> {
    /// Set the hash builder used to hash keys.
    ///
    /// Warning: `hash_builder` is normally randomly generated, and is designed
    /// to allow HashMaps to be resistant to attacks that cause many collisions
    /// and very poor performance. Setting it manually using this function can
    /// expose a DoS attack vector.
    ///
    /// The `hash_builder` passed should implement the [`BuildHasher`] trait for
    /// the HashMap to be useful, see its documentation for details.
    pub fn hasher<S>(self, hasher: S) -> HashMapBuilder<K, V, S> {
        HashMapBuilder {
            hasher,
            capacity: self.capacity,
            collector: self.collector,
            resize_mode: self.resize_mode,
            _kv: PhantomData,
        }
    }
}

impl<K, V, S> HashMapBuilder<K, V, S> {
    /// Set the initial capacity of the map.
    ///
    /// The table should be able to hold at least `capacity` elements before resizing.
    /// However, the capacity is an estimate, and the table may prematurely resize due
    /// to poor hash distribution. If `capacity` is 0, the hash map will not allocate.
    pub fn capacity(self, capacity: usize) -> HashMapBuilder<K, V, S> {
        HashMapBuilder {
            capacity,
            hasher: self.hasher,
            collector: self.collector,
            resize_mode: self.resize_mode,
            _kv: PhantomData,
        }
    }

    /// Set the resizing mode of the map. See [`ResizeMode`] for details.
    pub fn resize_mode(self, resize_mode: ResizeMode) -> Self {
        HashMapBuilder {
            resize_mode,
            hasher: self.hasher,
            capacity: self.capacity,
            collector: self.collector,
            _kv: PhantomData,
        }
    }

    /// Set the [`seize::Collector`] used for garbage collection.
    ///
    /// This method may be useful when you want more control over garbage collection.
    ///
    /// Note that all `Guard` references used to access the map must be produced by
    /// the provided `collector`.
    pub fn collector(self, collector: Collector) -> Self {
        HashMapBuilder {
            collector,
            hasher: self.hasher,
            capacity: self.capacity,
            resize_mode: self.resize_mode,
            _kv: PhantomData,
        }
    }

    /// Construct a [`HashMap`] from the builder, using the configured options.
    pub fn build(self) -> HashMap<K, V, S> {
        HashMap {
            raw: raw::HashMap::new(self.capacity, self.hasher, self.collector, self.resize_mode),
        }
    }
}

impl<K, V, S> fmt::Debug for HashMapBuilder<K, V, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HashMapBuilder")
            .field("capacity", &self.capacity)
            .field("collector", &self.collector)
            .field("resize_mode", &self.resize_mode)
            .finish()
    }
}

/// Resize behavior for a [`HashMap`].
///
/// Hash maps must resize when the underlying table becomes full, migrating all key and value pairs
/// to a new table. This type allows you to configure the resizing behavior when passed to
/// [`HashMapBuilder::resize_mode`].
#[derive(Debug)]
pub enum ResizeMode {
    /// Writers copy a constant number of key/value pairs to the new table before making
    /// progress.
    ///
    /// Incremental resizes avoids latency spikes that can occur when insert operations have
    /// to resize a large table. However, they reduce parallelism during the resize and so can reduce
    /// overall throughput. Incremental resizing also means reads or write operations during an
    /// in-progress resize may have to search both the current and new table before succeeding, trading
    /// off median latency during a resize for tail latency.
    ///
    /// This is the default resize mode, with a chunk size of `64`.
    Incremental(usize),
    /// All writes to the map must wait till the resize completes before making progress.
    ///
    /// Blocking resizes tend to be better in terms of throughput, especially in setups with
    /// multiple writers that can perform the resize in parallel. However, they can lead to latency
    /// spikes for insert operations that have to resize large tables.
    ///
    /// If insert latency is not a concern, such as if the keys in your map are stable, enabling blocking
    /// resizes may yield better performance.
    ///
    /// Blocking resizing may also be a better option if you rely heavily on iteration or similar
    /// operations, as they require completing any in-progress resizes for consistency.
    Blocking,
}

impl Default for ResizeMode {
    fn default() -> Self {
        // Incremental resizing is a good default for most workloads as it avoids
        // unexpected latency spikes.
        ResizeMode::Incremental(64)
    }
}

impl<K, V> HashMap<K, V> {
    /// Creates an empty `HashMap`.
    ///
    /// The hash map is initially created with a capacity of 0, so it will not allocate
    /// until it is first inserted into.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    /// let map: HashMap<&str, i32> = HashMap::new();
    /// ```
    pub fn new() -> HashMap<K, V> {
        HashMap::with_capacity_and_hasher(0, RandomState::new())
    }

    /// Creates an empty `HashMap` with the specified capacity.
    ///
    /// The table should be able to hold at least `capacity` elements before resizing.
    /// However, the capacity is an estimate, and the table may prematurely resize due
    /// to poor hash distribution. If `capacity` is 0, the hash map will not allocate.
    ///
    /// Note that the `HashMap` may grow and shrink as elements are inserted or removed,
    /// but it is guaranteed to never shrink below the initial capacity.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    /// let map: HashMap<&str, i32> = HashMap::with_capacity(10);
    /// ```
    pub fn with_capacity(capacity: usize) -> HashMap<K, V> {
        HashMap::with_capacity_and_hasher(capacity, RandomState::new())
    }

    /// Returns a builder for a `HashMap`.
    ///
    /// The builder can be used for more complex configuration, such as using
    /// a custom [`Collector`], or [`ResizeMode`].
    pub fn builder() -> HashMapBuilder<K, V> {
        HashMapBuilder {
            capacity: 0,
            hasher: RandomState::default(),
            collector: Collector::new(),
            resize_mode: ResizeMode::default(),
            _kv: PhantomData,
        }
    }
}

impl<K, V, S> Default for HashMap<K, V, S>
where
    S: Default,
{
    fn default() -> Self {
        HashMap::with_hasher(S::default())
    }
}

impl<K, V, S> HashMap<K, V, S> {
    /// Creates an empty `HashMap` which will use the given hash builder to hash
    /// keys.
    ///
    /// Warning: `hash_builder` is normally randomly generated, and is designed
    /// to allow HashMaps to be resistant to attacks that cause many collisions
    /// and very poor performance. Setting it manually using this function can
    /// expose a DoS attack vector.
    ///
    /// The `hash_builder` passed should implement the [`BuildHasher`] trait for
    /// the HashMap to be useful, see its documentation for details.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    /// use std::hash::RandomState;
    ///
    /// let s = RandomState::new();
    /// let map = HashMap::with_hasher(s);
    /// map.pin().insert(1, 2);
    /// ```
    pub fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
        HashMap::with_capacity_and_hasher(0, hash_builder)
    }

    /// Creates an empty `HashMap` with at least the specified capacity, using
    /// `hash_builder` to hash the keys.
    ///
    /// The table should be able to hold at least `capacity` elements before resizing.
    /// However, the capacity is an estimate, and the table may prematurely resize due
    /// to poor hash distribution. If `capacity` is 0, the hash map will not allocate.
    ///
    /// Warning: `hash_builder` is normally randomly generated, and is designed
    /// to allow HashMaps to be resistant to attacks that cause many collisions
    /// and very poor performance. Setting it manually using this function can
    /// expose a DoS attack vector.
    ///
    /// The `hasher` passed should implement the [`BuildHasher`] trait for
    /// the HashMap to be useful, see its documentation for details.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    /// use std::hash::RandomState;
    ///
    /// let s = RandomState::new();
    /// let map = HashMap::with_capacity_and_hasher(10, s);
    /// map.pin().insert(1, 2);
    /// ```
    pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> HashMap<K, V, S> {
        HashMap {
            raw: raw::HashMap::new(
                capacity,
                hash_builder,
                Collector::default(),
                ResizeMode::default(),
            ),
        }
    }

    /// Returns a pinned reference to the map.
    ///
    /// The returned reference manages a guard internally, preventing garbage collection
    /// for as long as it is held. See the [crate-level documentation](crate#usage) for details.
    #[inline]
    pub fn pin(&self) -> HashMapRef<'_, K, V, S, LocalGuard<'_>> {
        HashMapRef {
            guard: self.raw.guard(),
            map: self,
        }
    }

    /// Returns a pinned reference to the map.
    ///
    /// Unlike [`HashMap::pin`], the returned reference implements `Send` and `Sync`,
    /// allowing it to be held across `.await` points in work-stealing schedulers.
    /// This is especially useful for iterators.
    ///
    /// The returned reference manages a guard internally, preventing garbage collection
    /// for as long as it is held. See the [crate-level documentation](crate#usage) for details.
    #[inline]
    pub fn pin_owned(&self) -> HashMapRef<'_, K, V, S, OwnedGuard<'_>> {
        HashMapRef {
            guard: self.raw.owned_guard(),
            map: self,
        }
    }

    /// Returns a guard for use with this map.
    ///
    /// Note that holding on to a guard prevents garbage collection.
    /// See the [crate-level documentation](crate#usage) for details.
    #[inline]
    pub fn guard(&self) -> LocalGuard<'_> {
        self.raw.collector().enter()
    }

    /// Returns an owned guard for use with this map.
    ///
    /// Owned guards implement `Send` and `Sync`, allowing them to be held across
    /// `.await` points in work-stealing schedulers. This is especially useful
    /// for iterators.
    ///
    /// Note that holding on to a guard prevents garbage collection.
    /// See the [crate-level documentation](crate#usage) for details.
    #[inline]
    pub fn owned_guard(&self) -> OwnedGuard<'_> {
        self.raw.collector().enter_owned()
    }
}

impl<K, V, S> HashMap<K, V, S>
where
    K: Hash + Eq,
    S: BuildHasher,
{
    /// Returns the number of entries in the map.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    ///
    /// map.pin().insert(1, "a");
    /// map.pin().insert(2, "b");
    /// assert!(map.len() == 2);
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.raw.len()
    }

    /// Returns `true` if the map is empty. Otherwise returns `false`.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// assert!(map.is_empty());
    /// map.pin().insert("a", 1);
    /// assert!(!map.is_empty());
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns `true` if the map contains a value for the specified key.
    ///
    /// The key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// [`Eq`]: std::cmp::Eq
    /// [`Hash`]: std::hash::Hash
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// map.pin().insert(1, "a");
    /// assert_eq!(map.pin().contains_key(&1), true);
    /// assert_eq!(map.pin().contains_key(&2), false);
    /// ```
    #[inline]
    pub fn contains_key<Q>(&self, key: &Q, guard: &impl Guard) -> bool
    where
        Q: Equivalent<K> + Hash + ?Sized,
    {
        self.get(key, self.raw.verify(guard)).is_some()
    }

    /// Returns a reference to the value corresponding to the key.
    ///
    /// The key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// [`Eq`]: std::cmp::Eq
    /// [`Hash`]: std::hash::Hash
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// map.pin().insert(1, "a");
    /// assert_eq!(map.pin().get(&1), Some(&"a"));
    /// assert_eq!(map.pin().get(&2), None);
    /// ```
    #[inline]
    pub fn get<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<&'g V>
    where
        K: 'g,
        Q: Equivalent<K> + Hash + ?Sized,
    {
        match self.raw.get(key, self.raw.verify(guard)) {
            Some((_, v)) => Some(v),
            None => None,
        }
    }

    /// Returns the key-value pair corresponding to the supplied key.
    ///
    /// The supplied key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// [`Eq`]: std::cmp::Eq
    /// [`Hash`]: std::hash::Hash
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// map.pin().insert(1, "a");
    /// assert_eq!(map.pin().get_key_value(&1), Some((&1, &"a")));
    /// assert_eq!(map.pin().get_key_value(&2), None);
    /// ```
    #[inline]
    pub fn get_key_value<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<(&'g K, &'g V)>
    where
        Q: Equivalent<K> + Hash + ?Sized,
    {
        self.raw.get(key, self.raw.verify(guard))
    }

    /// Inserts a key-value pair 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 the old
    /// value is returned. The key is not updated, though; this matters for
    /// types that can be `==` without being identical. See the [standard library
    /// documentation] for details.
    ///
    /// [standard library documentation]: https://doc.rust-lang.org/std/collections/index.html#insert-and-complex-keys
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// assert_eq!(map.pin().insert(37, "a"), None);
    /// assert_eq!(map.pin().is_empty(), false);
    ///
    /// map.pin().insert(37, "b");
    /// assert_eq!(map.pin().insert(37, "c"), Some(&"b"));
    /// assert_eq!(map.pin().get(&37), Some(&"c"));
    /// ```
    #[inline]
    pub fn insert<'g>(&self, key: K, value: V, guard: &'g impl Guard) -> Option<&'g V> {
        match self.raw.insert(key, value, true, self.raw.verify(guard)) {
            InsertResult::Inserted(_) => None,
            InsertResult::Replaced(value) => Some(value),
            InsertResult::Error { .. } => unreachable!(),
        }
    }

    /// Tries to insert a key-value pair into the map, and returns
    /// a reference to the value that was inserted.
    ///
    /// If the map already had this key present, nothing is updated, and
    /// an error containing the existing value is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// let map = map.pin();
    ///
    /// assert_eq!(map.try_insert(37, "a").unwrap(), &"a");
    ///
    /// let err = map.try_insert(37, "b").unwrap_err();
    /// assert_eq!(err.current, &"a");
    /// assert_eq!(err.not_inserted, "b");
    /// ```
    #[inline]
    pub fn try_insert<'g>(
        &self,
        key: K,
        value: V,
        guard: &'g impl Guard,
    ) -> Result<&'g V, OccupiedError<'g, V>> {
        // Safety: Checked the guard above.
        match self.raw.insert(key, value, false, self.raw.verify(guard)) {
            InsertResult::Inserted(value) => Ok(value),
            InsertResult::Error {
                current,
                not_inserted,
            } => Err(OccupiedError {
                current,
                not_inserted,
            }),
            InsertResult::Replaced(_) => unreachable!(),
        }
    }

    /// Tries to insert a key and value computed from a closure into the map,
    /// and returns a reference to the value that was inserted.
    ///
    /// If the map already had this key present, nothing is updated, and
    /// the existing value is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// let map = map.pin();
    ///
    /// assert_eq!(map.try_insert_with(37, || "a").unwrap(), &"a");
    ///
    /// let current = map.try_insert_with(37, || "b").unwrap_err();
    /// assert_eq!(current, &"a");
    /// ```
    #[inline]
    pub fn try_insert_with<'g, F>(
        &self,
        key: K,
        f: F,
        guard: &'g impl Guard,
    ) -> Result<&'g V, &'g V>
    where
        F: FnOnce() -> V,
        K: 'g,
    {
        self.raw.try_insert_with(key, f, self.raw.verify(guard))
    }

    /// Returns a reference to the value corresponding to the key, or inserts a default value.
    ///
    /// If the given key is present, the corresponding value is returned. If it is not present,
    /// the provided `value` is inserted, and a reference to the newly inserted value is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// assert_eq!(map.pin().get_or_insert("a", 3), &3);
    /// assert_eq!(map.pin().get_or_insert("a", 6), &3);
    /// ```
    #[inline]
    pub fn get_or_insert<'g>(&self, key: K, value: V, guard: &'g impl Guard) -> &'g V {
        // Note that we use `try_insert` instead of `compute` or `get_or_insert_with` here, as it
        // allows us to avoid the closure indirection.
        match self.try_insert(key, value, guard) {
            Ok(inserted) => inserted,
            Err(OccupiedError { current, .. }) => current,
        }
    }

    /// Returns a reference to the value corresponding to the key, or inserts a default value
    /// computed from a closure.
    ///
    /// If the given key is present, the corresponding value is returned. If it is not present,
    /// the value computed from `f` is inserted, and a reference to the newly inserted value is
    /// returned.
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// assert_eq!(map.pin().get_or_insert_with("a", || 3), &3);
    /// assert_eq!(map.pin().get_or_insert_with("a", || 6), &3);
    /// ```
    #[inline]
    pub fn get_or_insert_with<'g, F>(&self, key: K, f: F, guard: &'g impl Guard) -> &'g V
    where
        F: FnOnce() -> V,
        K: 'g,
    {
        self.raw.get_or_insert_with(key, f, self.raw.verify(guard))
    }

    /// Updates an existing entry atomically.
    ///
    /// If the value for the specified `key` is present, the new value is computed and stored the
    /// using the provided update function, and the new value is returned. Otherwise, `None`
    /// is returned.
    ///
    ///
    /// The update function is given the current value associated with the given key and returns the
    /// new value to be stored. The operation is applied atomically only if the state of the entry remains
    /// the same, meaning that it is not concurrently modified in any way. If the entry is
    /// modified, the operation is retried with the new entry, similar to a traditional [compare-and-swap](https://en.wikipedia.org/wiki/Compare-and-swap)
    /// operation.
    ///
    /// Note that the `update` function should be pure as it may be called multiple times, and the output
    /// for a given entry may be memoized across retries.
    ///
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// map.pin().insert("a", 1);
    /// assert_eq!(map.pin().get(&"a"), Some(&1));
    ///
    /// map.pin().update("a", |v| v + 1);
    /// assert_eq!(map.pin().get(&"a"), Some(&2));
    /// ```
    #[inline]
    pub fn update<'g, F>(&self, key: K, update: F, guard: &'g impl Guard) -> Option<&'g V>
    where
        F: Fn(&V) -> V,
        K: 'g,
    {
        self.raw.update(key, update, self.raw.verify(guard))
    }

    /// Updates an existing entry or inserts a default value.
    ///
    /// If the value for the specified `key` is present, the new value is computed and stored the
    /// using the provided update function, and the new value is returned. Otherwise, the provided
    /// `value` is inserted into the map, and a reference to the newly inserted value is returned.
    ///
    /// See [`HashMap::update`] for details about how atomic updates are performed.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// assert_eq!(*map.pin().update_or_insert("a", |i| i + 1, 0), 0);
    /// assert_eq!(*map.pin().update_or_insert("a", |i| i + 1, 0), 1);
    /// ```
    #[inline]
    pub fn update_or_insert<'g, F>(
        &self,
        key: K,
        update: F,
        value: V,
        guard: &'g impl Guard,
    ) -> &'g V
    where
        F: Fn(&V) -> V,
        K: 'g,
    {
        self.update_or_insert_with(key, update, || value, guard)
    }

    /// Updates an existing entry or inserts a default value computed from a closure.
    ///
    /// If the value for the specified `key` is present, the new value is computed and stored the
    /// using the provided update function, and the new value is returned. Otherwise, the value
    /// computed by `f` is inserted into the map, and a reference to the newly inserted value is
    /// returned.
    ///
    /// See [`HashMap::update`] for details about how atomic updates are performed.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// assert_eq!(*map.pin().update_or_insert_with("a", |i| i + 1, || 0), 0);
    /// assert_eq!(*map.pin().update_or_insert_with("a", |i| i + 1, || 0), 1);
    /// ```
    #[inline]
    pub fn update_or_insert_with<'g, U, F>(
        &self,
        key: K,
        update: U,
        f: F,
        guard: &'g impl Guard,
    ) -> &'g V
    where
        F: FnOnce() -> V,
        U: Fn(&V) -> V,
        K: 'g,
    {
        self.raw
            .update_or_insert_with(key, update, f, self.raw.verify(guard))
    }

    /// Updates an entry with a compare-and-swap (CAS) function.
    ///
    /// This method allows you to perform complex operations on the map atomically. The `compute`
    /// closure is given the current state of the entry and returns the operation that should be
    /// performed. The operation is applied atomically only if the state of the entry remains the same,
    /// meaning it is not concurrently modified in any way.
    ///
    /// Note that the `compute` function should be pure as it may be called multiple times, and
    /// the output for a given entry may be memoized across retries.
    ///
    /// In most cases you can avoid this method and instead use a higher-level atomic operation.
    /// See the [crate-level documentation](crate#atomic-operations) for details.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use papaya::{HashMap, Operation, Compute};
    ///
    /// let map = HashMap::new();
    /// let map = map.pin();
    ///
    /// let compute = |entry| match entry {
    ///     // Remove the value if it is even.
    ///     Some((_key, value)) if value % 2 == 0 => {
    ///         Operation::Remove
    ///     }
    ///
    ///     // Increment the value if it is odd.
    ///     Some((_key, value)) => {
    ///         Operation::Insert(value + 1)
    ///     }
    ///
    ///     // Do nothing if the key does not exist
    ///     None => Operation::Abort(()),
    /// };
    ///
    /// assert_eq!(map.compute('A', compute), Compute::Aborted(()));
    ///
    /// map.insert('A', 1);
    /// assert_eq!(map.compute('A', compute), Compute::Updated {
    ///     old: (&'A', &1),
    ///     new: (&'A', &2),
    /// });
    /// assert_eq!(map.compute('A', compute), Compute::Removed(&'A', &2));
    /// ```
    #[inline]
    pub fn compute<'g, F, T>(
        &self,
        key: K,
        compute: F,
        guard: &'g impl Guard,
    ) -> Compute<'g, K, V, T>
    where
        F: FnMut(Option<(&'g K, &'g V)>) -> Operation<V, T>,
    {
        self.raw.compute(key, compute, self.raw.verify(guard))
    }

    /// Removes a key from the map, returning the value at the key if the key
    /// was previously in the map.
    ///
    /// The key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// map.pin().insert(1, "a");
    /// assert_eq!(map.pin().remove(&1), Some(&"a"));
    /// assert_eq!(map.pin().remove(&1), None);
    /// ```
    #[inline]
    pub fn remove<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<&'g V>
    where
        K: 'g,
        Q: Equivalent<K> + Hash + ?Sized,
    {
        match self.raw.remove(key, self.raw.verify(guard)) {
            Some((_, value)) => Some(value),
            None => None,
        }
    }

    /// Removes a key from the map, returning the stored key and value if the
    /// key was previously in the map.
    ///
    /// The key may be any borrowed form of the map's key type, but
    /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
    /// the key type.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// map.pin().insert(1, "a");
    /// assert_eq!(map.pin().get(&1), Some(&"a"));
    /// assert_eq!(map.pin().remove_entry(&1), Some((&1, &"a")));
    /// assert_eq!(map.pin().remove(&1), None);
    /// ```
    #[inline]
    pub fn remove_entry<'g, Q>(&self, key: &Q, guard: &'g impl Guard) -> Option<(&'g K, &'g V)>
    where
        K: 'g,
        Q: Equivalent<K> + Hash + ?Sized,
    {
        self.raw.remove(key, self.raw.verify(guard))
    }

    /// Conditionally removes a key from the map based on the provided closure.
    ///
    /// If the key is found in the map and the closure returns `true` given the key and its value,
    /// the key and value are returned successfully. Note that the returned entry is guaranteed to
    /// be the same entry that the closure returned `true` for. However, the closure returning `true`
    /// does not guarantee that the entry is removed in the presence of concurrent modifications.
    ///
    /// If the key is not found in the map, `Ok(None)` is returned.
    ///
    /// If the closure returns `false`, an error is returned containing the entry provided to the closure.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    /// map.pin().insert(1, "a");
    ///
    /// assert_eq!(map.pin().remove_if(&1, |k, v| *v == "b"), Err((&1, &"a")));
    /// assert_eq!(map.pin().get(&1), Some(&"a"));
    /// assert_eq!(map.pin().remove_if(&1, |k, v| *v == "a"), Ok(Some((&1, &"a"))));
    /// assert_eq!(map.pin().remove_if(&1, |_, _| true), Ok(None));
    /// ```
    #[inline]
    pub fn remove_if<'g, Q, F>(
        &self,
        key: &Q,
        should_remove: F,
        guard: &'g impl Guard,
    ) -> Result<Option<(&'g K, &'g V)>, (&'g K, &'g V)>
    where
        Q: Equivalent<K> + Hash + ?Sized,
        F: FnMut(&K, &V) -> bool,
    {
        self.raw
            .remove_if(key, should_remove, self.raw.verify(guard))
    }

    /// Tries to reserve capacity for `additional` more elements to be inserted
    /// in the `HashMap`.
    ///
    /// After calling this method, the table should be able to hold at least `capacity` elements
    /// before resizing. However, the capacity is an estimate, and the table may prematurely resize
    /// due to poor hash distribution. The collection may also reserve more space to avoid frequent
    /// reallocations.
    ///
    /// # Panics
    ///
    /// Panics if the new allocation size overflows `usize`.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map: HashMap<&str, i32> = HashMap::new();
    /// map.pin().reserve(10);
    /// ```
    #[inline]
    pub fn reserve(&self, additional: usize, guard: &impl Guard) {
        self.raw.reserve(additional, self.raw.verify(guard))
    }

    /// Clears the map, removing all key-value pairs.
    ///
    /// Note that this method will block until any in-progress resizes are
    /// completed before proceeding. See the [consistency](crate#consistency)
    /// section for details.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::new();
    ///
    /// map.pin().insert(1, "a");
    /// map.pin().clear();
    /// assert!(map.pin().is_empty());
    /// ```
    #[inline]
    pub fn clear(&self, guard: &impl Guard) {
        self.raw.clear(self.raw.verify(guard))
    }

    /// Retains only the elements specified by the predicate.
    ///
    /// In other words, remove all pairs `(k, v)` for which `f(&k, &v)` returns `false`.
    /// The elements are visited in unsorted (and unspecified) order.
    ///
    /// Note the function may be called more than once for a given key if its value is
    /// concurrently modified during removal.
    ///
    /// Additionally, this method will block until any in-progress resizes are
    /// completed before proceeding. See the [consistency](crate#consistency)
    /// section for details.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let mut map: HashMap<i32, i32> = (0..8).map(|x| (x, x * 10)).collect();
    /// map.pin().retain(|&k, _| k % 2 == 0);
    /// assert_eq!(map.len(), 4);
    /// ```
    #[inline]
    pub fn retain<F>(&self, f: F, guard: &impl Guard)
    where
        F: FnMut(&K, &V) -> bool,
    {
        self.raw.retain(f, self.raw.verify(guard))
    }

    /// An iterator visiting all key-value pairs in arbitrary order.
    /// The iterator element type is `(&K, &V)`.
    ///
    /// Note that this method will block until any in-progress resizes are
    /// completed before proceeding. See the [consistency](crate#consistency)
    /// section for details.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// for (key, val) in map.pin().iter() {
    ///     println!("key: {key} val: {val}");
    /// }
    #[inline]
    pub fn iter<'g, G>(&self, guard: &'g G) -> Iter<'g, K, V, G>
    where
        G: Guard,
    {
        Iter {
            raw: self.raw.iter(self.raw.verify(guard)),
        }
    }

    /// An iterator visiting all keys in arbitrary order.
    /// The iterator element type is `&K`.
    ///
    /// Note that this method will block until any in-progress resizes are
    /// completed before proceeding. See the [consistency](crate#consistency)
    /// section for details.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// for key in map.pin().keys() {
    ///     println!("{key}");
    /// }
    /// ```
    #[inline]
    pub fn keys<'g, G>(&self, guard: &'g G) -> Keys<'g, K, V, G>
    where
        G: Guard,
    {
        Keys {
            iter: self.iter(guard),
        }
    }

    /// An iterator visiting all values in arbitrary order.
    /// The iterator element type is `&V`.
    ///
    /// Note that this method will block until any in-progress resizes are
    /// completed before proceeding. See the [consistency](crate#consistency)
    /// section for details.
    ///
    /// # Examples
    ///
    /// ```
    /// use papaya::HashMap;
    ///
    /// let map = HashMap::from([
    ///     ("a", 1),
    ///     ("b", 2),
    ///     ("c", 3),
    /// ]);
    ///
    /// for value in map.pin().values() {
    ///     println!("{value}");
    /// }
    /// ```
    #[inline]
    pub fn values<'g, G>(&self, guard: &'g G) -> Values<'g, K, V, G>
    where
        G: Guard,
    {
        Values {
            iter: self.iter(guard),
        }
    }
}

/// An operation to perform on given entry in a [`HashMap`].
///
/// See [`HashMap::compute`] for details.
#[derive(Debug, PartialEq, Eq)]
pub enum Operation<V, T> {
    /// Insert the given value.
    Insert(V),

    /// Remove the entry from the map.
    Remove,

    /// Abort the operation with the given value.
    Abort(T),
}

/// The result of a [`compute`](HashMap::compute) operation.
///
/// Contains information about the [`Operation`] that was performed.
#[derive(Debug, PartialEq, Eq)]
pub enum Compute<'g, K, V, T> {
    /// The given entry was inserted.
    Inserted(&'g K, &'g V),

    /// The entry was updated.
    Updated {
        /// The entry that was replaced.
        old: (&'g K, &'g V),

        /// The entry that was inserted.
        new: (&'g K, &'g V),
    },

    /// The given entry was removed.
    Removed(&'g K, &'g V),

    /// The operation was aborted with the given value.
    Aborted(T),
}

/// An error returned by [`try_insert`](HashMap::try_insert) when the key already exists.
///
/// Contains the existing value, and the value that was not inserted.
#[derive(Debug, PartialEq, Eq)]
pub struct OccupiedError<'a, V: 'a> {
    /// The value in the map that was already present.
    pub current: &'a V,
    /// The value which was not inserted, because the entry was already occupied.
    pub not_inserted: V,
}

impl<K, V, S> PartialEq for HashMap<K, V, S>
where
    K: Hash + Eq,
    V: PartialEq,
    S: BuildHasher,
{
    fn eq(&self, other: &Self) -> bool {
        if self.len() != other.len() {
            return false;
        }

        let (guard1, guard2) = (&self.guard(), &other.guard());

        let mut iter = self.iter(guard1);
        iter.all(|(key, value)| other.get(key, guard2).is_some_and(|v| *value == *v))
    }
}

impl<K, V, S> Eq for HashMap<K, V, S>
where
    K: Hash + Eq,
    V: Eq,
    S: BuildHasher,
{
}

impl<K, V, S> fmt::Debug for HashMap<K, V, S>
where
    K: Hash + Eq + fmt::Debug,
    V: fmt::Debug,
    S: BuildHasher,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let guard = self.guard();
        f.debug_map().entries(self.iter(&guard)).finish()
    }
}

impl<K, V, S> Extend<(K, V)> for &HashMap<K, V, S>
where
    K: Hash + Eq,
    S: BuildHasher,
{
    fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T) {
        // from `hashbrown::HashMap::extend`:
        // Keys may be already present or show multiple times in the iterator.
        // Reserve the entire hint lower bound if the map is empty.
        // Otherwise reserve half the hint (rounded up), so the map
        // will only resize twice in the worst case.
        let iter = iter.into_iter();
        let reserve = if self.is_empty() {
            iter.size_hint().0
        } else {
            (iter.size_hint().0 + 1) / 2
        };

        let guard = self.guard();
        self.reserve(reserve, &guard);

        for (key, value) in iter {
            self.insert(key, value, &guard);
        }
    }
}

impl<'a, K, V, S> Extend<(&'a K, &'a V)> for &HashMap<K, V, S>
where
    K: Copy + Hash + Eq,
    V: Copy,
    S: BuildHasher,
{
    fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T) {
        self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
    }
}

impl<K, V, const N: usize> From<[(K, V); N]> for HashMap<K, V, RandomState>
where
    K: Hash + Eq,
{
    fn from(arr: [(K, V); N]) -> Self {
        HashMap::from_iter(arr)
    }
}

impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
where
    K: Hash + Eq,
    S: BuildHasher + Default,
{
    fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
        let mut iter = iter.into_iter();

        if let Some((key, value)) = iter.next() {
            let (lower, _) = iter.size_hint();
            let map = HashMap::with_capacity_and_hasher(lower.saturating_add(1), S::default());

            // Ideally we could use an unprotected guard here. However, `insert`
            // returns references to values that were replaced and retired, so
            // we need a "real" guard. A `raw_insert` method that strictly returns
            // pointers would fix this.
            {
                let map = map.pin();
                map.insert(key, value);
                for (key, value) in iter {
                    map.insert(key, value);
                }
            }

            map
        } else {
            Self::default()
        }
    }
}

impl<K, V, S> Clone for HashMap<K, V, S>
where
    K: Clone + Hash + Eq,
    V: Clone,
    S: BuildHasher + Clone,
{
    fn clone(&self) -> HashMap<K, V, S> {
        let other = HashMap::builder()
            .capacity(self.len())
            .hasher(self.raw.hasher.clone())
            .collector(seize::Collector::new())
            .build();

        {
            let (guard1, guard2) = (&self.guard(), &other.guard());
            for (key, value) in self.iter(guard1) {
                other.insert(key.clone(), value.clone(), guard2);
            }
        }

        other
    }
}

/// A pinned reference to a [`HashMap`].
///
/// This type is created with [`HashMap::pin`] and can be used to easily access a [`HashMap`]
/// without explicitly managing a guard. See the [crate-level documentation](crate#usage) for details.
pub struct HashMapRef<'map, K, V, S, G> {
    guard: MapGuard<G>,
    map: &'map HashMap<K, V, S>,
}

impl<'map, K, V, S, G> HashMapRef<'map, K, V, S, G>
where
    K: Hash + Eq,
    S: BuildHasher,
    G: Guard,
{
    /// Returns a reference to the inner [`HashMap`].
    #[inline]
    pub fn map(&self) -> &'map HashMap<K, V, S> {
        self.map
    }

    /// Returns the number of entries in the map.
    ///
    /// See [`HashMap::len`] for details.
    #[inline]
    pub fn len(&self) -> usize {
        self.map.raw.len()
    }

    /// Returns `true` if the map is empty. Otherwise returns `false`.
    ///
    /// See [`HashMap::is_empty`] for details.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns `true` if the map contains a value for the specified key.
    ///
    /// See [`HashMap::contains_key`] for details.
    #[inline]
    pub fn contains_key<Q>(&self, key: &Q) -> bool
    where
        Q: Equivalent<K> + Hash + ?Sized,
    {
        self.get(key).is_some()
    }

    /// Returns a reference to the value corresponding to the key.
    ///
    /// See [`HashMap::get`] for details.
    #[inline]
    pub fn get<Q>(&self, key: &Q) -> Option<&V>
    where
        Q: Equivalent<K> + Hash + ?Sized,
    {
        match self.map.raw.get(key, &self.guard) {
            Some((_, v)) => Some(v),
            None => None,
        }
    }

    /// Returns the key-value pair corresponding to the supplied key.
    ///
    /// See [`HashMap::get_key_value`] for details.
    #[inline]
    pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
    where
        Q: Equivalent<K> + Hash + ?Sized,
    {
        self.map.raw.get(key, &self.guard)
    }

    /// Inserts a key-value pair into the map.
    ///
    /// See [`HashMap::insert`] for details.
    #[inline]
    pub fn insert(&self, key: K, value: V) -> Option<&V> {
        match self.map.raw.insert(key, value, true, &self.guard) {
            InsertResult::Inserted(_) => None,
            InsertResult::Replaced(value) => Some(value),
            InsertResult::Error { .. } => unreachable!(),
        }
    }

    /// Tries to insert a key-value pair into the map, and returns
    /// a reference to the value that was inserted.
    ///
    /// See [`HashMap::try_insert`] for details.
    #[inline]
    pub fn try_insert(&self, key: K, value: V) -> Result<&V, OccupiedError<'_, V>> {
        match self.map.raw.insert(key, value, false, &self.guard) {
            InsertResult::Inserted(value) => Ok(value),
            InsertResult::Error {
                current,
                not_inserted,
            } => Err(OccupiedError {
                current,
                not_inserted,
            }),
            InsertResult::Replaced(_) => unreachable!(),
        }
    }

    /// Tries to insert a key and value computed from a closure into the map,
    /// and returns a reference to the value that was inserted.
    ///
    /// See [`HashMap::try_insert_with`] for details.
    #[inline]
    pub fn try_insert_with<F>(&self, key: K, f: F) -> Result<&V, &V>
    where
        F: FnOnce() -> V,
    {
        self.map.raw.try_insert_with(key, f, &self.guard)
    }

    /// Returns a reference to the value corresponding to the key, or inserts a default value.
    ///
    /// See [`HashMap::get_or_insert`] for details.
    #[inline]
    pub fn get_or_insert(&self, key: K, value: V) -> &V {
        // Note that we use `try_insert` instead of `compute` or `get_or_insert_with` here, as it
        // allows us to avoid the closure indirection.
        match self.try_insert(key, value) {
            Ok(inserted) => inserted,
            Err(OccupiedError { current, .. }) => current,
        }
    }

    /// Returns a reference to the value corresponding to the key, or inserts a default value
    /// computed from a closure.
    ///
    /// See [`HashMap::get_or_insert_with`] for details.
    #[inline]
    pub fn get_or_insert_with<F>(&self, key: K, f: F) -> &V
    where
        F: FnOnce() -> V,
    {
        self.map.raw.get_or_insert_with(key, f, &self.guard)
    }

    /// Updates an existing entry atomically.
    ///
    /// See [`HashMap::update`] for details.
    #[inline]
    pub fn update<F>(&self, key: K, update: F) -> Option<&V>
    where
        F: Fn(&V) -> V,
    {
        self.map.raw.update(key, update, &self.guard)
    }

    /// Updates an existing entry or inserts a default value.
    ///
    /// See [`HashMap::update_or_insert`] for details.
    #[inline]
    pub fn update_or_insert<F>(&self, key: K, update: F, value: V) -> &V
    where
        F: Fn(&V) -> V,
    {
        self.update_or_insert_with(key, update, || value)
    }

    /// Updates an existing entry or inserts a default value computed from a closure.
    ///
    /// See [`HashMap::update_or_insert_with`] for details.
    #[inline]
    pub fn update_or_insert_with<U, F>(&self, key: K, update: U, f: F) -> &V
    where
        F: FnOnce() -> V,
        U: Fn(&V) -> V,
    {
        self.map
            .raw
            .update_or_insert_with(key, update, f, &self.guard)
    }

    // Updates an entry with a compare-and-swap (CAS) function.
    //
    /// See [`HashMap::compute`] for details.
    #[inline]
    pub fn compute<'g, F, T>(&'g self, key: K, compute: F) -> Compute<'g, K, V, T>
    where
        F: FnMut(Option<(&'g K, &'g V)>) -> Operation<V, T>,
    {
        self.map.raw.compute(key, compute, &self.guard)
    }

    /// Removes a key from the map, returning the value at the key if the key
    /// was previously in the map.
    ///
    /// See [`HashMap::remove`] for details.
    #[inline]
    pub fn remove<Q>(&self, key: &Q) -> Option<&V>
    where
        Q: Equivalent<K> + Hash + ?Sized,
    {
        match self.map.raw.remove(key, &self.guard) {
            Some((_, value)) => Some(value),
            None => None,
        }
    }

    /// Removes a key from the map, returning the stored key and value if the
    /// key was previously in the map.
    ///
    /// See [`HashMap::remove_entry`] for details.
    #[inline]
    pub fn remove_entry<Q>(&self, key: &Q) -> Option<(&K, &V)>
    where
        Q: Equivalent<K> + Hash + ?Sized,
    {
        self.map.raw.remove(key, &self.guard)
    }

    /// Conditionally removes a key from the map based on the provided closure.
    ///
    /// See [`HashMap::remove_if`] for details.
    #[inline]
    pub fn remove_if<Q, F>(&self, key: &Q, should_remove: F) -> Result<Option<(&K, &V)>, (&K, &V)>
    where
        Q: Equivalent<K> + Hash + ?Sized,
        F: FnMut(&K, &V) -> bool,
    {
        self.map.raw.remove_if(key, should_remove, &self.guard)
    }

    /// Clears the map, removing all key-value pairs.
    ///
    /// See [`HashMap::clear`] for details.
    #[inline]
    pub fn clear(&self) {
        self.map.raw.clear(&self.guard)
    }

    /// Retains only the elements specified by the predicate.
    ///
    /// See [`HashMap::retain`] for details.
    #[inline]
    pub fn retain<F>(&self, f: F)
    where
        F: FnMut(&K, &V) -> bool,
    {
        self.map.raw.retain(f, &self.guard)
    }

    /// Tries to reserve capacity for `additional` more elements to be inserted
    /// in the map.
    ///
    /// See [`HashMap::reserve`] for details.
    #[inline]
    pub fn reserve(&self, additional: usize) {
        self.map.raw.reserve(additional, &self.guard)
    }

    /// An iterator visiting all key-value pairs in arbitrary order.
    /// The iterator element type is `(&K, &V)`.
    ///
    /// See [`HashMap::iter`] for details.
    #[inline]
    pub fn iter(&self) -> Iter<'_, K, V, G> {
        Iter {
            raw: self.map.raw.iter(&self.guard),
        }
    }

    /// An iterator visiting all keys in arbitrary order.
    /// The iterator element type is `&K`.
    ///
    /// See [`HashMap::keys`] for details.
    #[inline]
    pub fn keys(&self) -> Keys<'_, K, V, G> {
        Keys { iter: self.iter() }
    }

    /// An iterator visiting all values in arbitrary order.
    /// The iterator element type is `&V`.
    ///
    /// See [`HashMap::values`] for details.
    #[inline]
    pub fn values(&self) -> Values<'_, K, V, G> {
        Values { iter: self.iter() }
    }
}

impl<K, V, S, G> fmt::Debug for HashMapRef<'_, K, V, S, G>
where
    K: Hash + Eq + fmt::Debug,
    V: fmt::Debug,
    S: BuildHasher,
    G: Guard,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_map().entries(self.iter()).finish()
    }
}

impl<'a, K, V, S, G> IntoIterator for &'a HashMapRef<'_, K, V, S, G>
where
    K: Hash + Eq,
    S: BuildHasher,
    G: Guard,
{
    type Item = (&'a K, &'a V);
    type IntoIter = Iter<'a, K, V, G>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// An iterator over a map's entries.
///
/// This struct is created by the [`iter`](HashMap::iter) method on [`HashMap`]. See its documentation for details.
pub struct Iter<'g, K, V, G> {
    raw: raw::Iter<'g, K, V, MapGuard<G>>,
}

impl<'g, K: 'g, V: 'g, G> Iterator for Iter<'g, K, V, G>
where
    G: Guard,
{
    type Item = (&'g K, &'g V);

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.raw.next()
    }
}

impl<K, V, G> fmt::Debug for Iter<'_, K, V, G>
where
    K: fmt::Debug,
    V: fmt::Debug,
    G: Guard,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list()
            .entries(Iter {
                raw: self.raw.clone(),
            })
            .finish()
    }
}

/// An iterator over a map's keys.
///
/// This struct is created by the [`keys`](HashMap::keys) method on [`HashMap`]. See its documentation for details.
pub struct Keys<'g, K, V, G> {
    iter: Iter<'g, K, V, G>,
}

impl<'g, K: 'g, V: 'g, G> Iterator for Keys<'g, K, V, G>
where
    G: Guard,
{
    type Item = &'g K;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let (key, _) = self.iter.next()?;
        Some(key)
    }
}

impl<K, V, G> fmt::Debug for Keys<'_, K, V, G>
where
    K: fmt::Debug,
    V: fmt::Debug,
    G: Guard,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Keys").field(&self.iter).finish()
    }
}

/// An iterator over a map's values.
///
/// This struct is created by the [`values`](HashMap::values) method on [`HashMap`]. See its documentation for details.
pub struct Values<'g, K, V, G> {
    iter: Iter<'g, K, V, G>,
}

impl<'g, K: 'g, V: 'g, G> Iterator for Values<'g, K, V, G>
where
    G: Guard,
{
    type Item = &'g V;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let (_, value) = self.iter.next()?;
        Some(value)
    }
}

impl<K, V, G> fmt::Debug for Values<'_, K, V, G>
where
    K: fmt::Debug,
    V: fmt::Debug,
    G: Guard,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Values").field(&self.iter).finish()
    }
}