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
#![allow(clippy::type_complexity)]
#[cfg(feature = "arbitrary")]
mod arbitrary;
pub mod iter;
pub mod iter_set;
mod lock;
pub mod mapref;
mod read_only;
#[cfg(feature = "serde")]
mod serde;
mod set;
pub mod setref;
mod t;
pub mod try_result;
mod util;
#[cfg(feature = "rayon")]
pub mod rayon {
pub mod map;
pub mod read_only;
pub mod set;
}
#[cfg(not(feature = "raw-api"))]
use crate::lock::{RwLock, RwLockReadGuard, RwLockWriteGuard};
#[cfg(feature = "raw-api")]
pub use crate::lock::{RawRwLock, RwLock, RwLockReadGuard, RwLockWriteGuard};
use cfg_if::cfg_if;
use core::borrow::Borrow;
use core::fmt;
use core::hash::{BuildHasher, Hash, Hasher};
use core::iter::FromIterator;
use core::ops::{BitAnd, BitOr, Shl, Shr, Sub};
use iter::{Iter, IterMut, OwningIter};
use mapref::entry::{Entry, OccupiedEntry, VacantEntry};
use mapref::multiple::RefMulti;
use mapref::one::{Ref, RefMut};
use once_cell::sync::OnceCell;
pub use read_only::ReadOnlyView;
pub use set::DashSet;
use std::collections::hash_map::RandomState;
pub use t::Map;
use try_result::TryResult;
cfg_if! {
if #[cfg(feature = "raw-api")] {
pub use util::SharedValue;
} else {
use util::SharedValue;
}
}
pub(crate) type HashMap<K, V, S> = hashbrown::HashMap<K, SharedValue<V>, S>;
// Temporary reimplementation of [`std::collections::TryReserveError`]
// util [`std::collections::TryReserveError`] stabilises.
// We cannot easily create `std::collections` error type from `hashbrown` error type
// without access to `TryReserveError::kind` method.
#[non_exhaustive]
#[derive(Clone, PartialEq, Eq, Debug)]
pub struct TryReserveError {}
fn default_shard_amount() -> usize {
static DEFAULT_SHARD_AMOUNT: OnceCell<usize> = OnceCell::new();
*DEFAULT_SHARD_AMOUNT.get_or_init(|| {
(std::thread::available_parallelism().map_or(1, usize::from) * 4).next_power_of_two()
})
}
fn ncb(shard_amount: usize) -> usize {
shard_amount.trailing_zeros() as usize
}
/// DashMap is an implementation of a concurrent associative array/hashmap in Rust.
///
/// DashMap tries to implement an easy to use API similar to `std::collections::HashMap`
/// with some slight changes to handle concurrency.
///
/// DashMap tries to be very simple to use and to be a direct replacement for `RwLock<HashMap<K, V, S>>`.
/// To accomplish this, all methods take `&self` instead of modifying methods taking `&mut self`.
/// This allows you to put a DashMap in an `Arc<T>` and share it between threads while being able to modify it.
///
/// Documentation mentioning locking behaviour acts in the reference frame of the calling thread.
/// This means that it is safe to ignore it across multiple threads.
pub struct DashMap<K, V, S = RandomState> {
shift: usize,
shards: Box<[RwLock<HashMap<K, V, S>>]>,
hasher: S,
}
impl<K: Eq + Hash + Clone, V: Clone, S: Clone> Clone for DashMap<K, V, S> {
fn clone(&self) -> Self {
let mut inner_shards = Vec::new();
for shard in self.shards.iter() {
let shard = shard.read();
inner_shards.push(RwLock::new((*shard).clone()));
}
Self {
shift: self.shift,
shards: inner_shards.into_boxed_slice(),
hasher: self.hasher.clone(),
}
}
}
impl<K, V, S> Default for DashMap<K, V, S>
where
K: Eq + Hash,
S: Default + BuildHasher + Clone,
{
fn default() -> Self {
Self::with_hasher(Default::default())
}
}
impl<'a, K: 'a + Eq + Hash, V: 'a> DashMap<K, V, RandomState> {
/// Creates a new DashMap with a capacity of 0.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let reviews = DashMap::new();
/// reviews.insert("Veloren", "What a fantastic game!");
/// ```
pub fn new() -> Self {
DashMap::with_hasher(RandomState::default())
}
/// Creates a new DashMap with a specified starting capacity.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let mappings = DashMap::with_capacity(2);
/// mappings.insert(2, 4);
/// mappings.insert(8, 16);
/// ```
pub fn with_capacity(capacity: usize) -> Self {
DashMap::with_capacity_and_hasher(capacity, RandomState::default())
}
/// Creates a new DashMap with a specified shard amount
///
/// shard_amount should greater than 0 and be a power of two.
/// If a shard_amount which is not a power of two is provided, the function will panic.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let mappings = DashMap::with_shard_amount(32);
/// mappings.insert(2, 4);
/// mappings.insert(8, 16);
/// ```
pub fn with_shard_amount(shard_amount: usize) -> Self {
Self::with_capacity_and_hasher_and_shard_amount(0, RandomState::default(), shard_amount)
}
/// Creates a new DashMap with a specified capacity and shard amount.
///
/// shard_amount should greater than 0 and be a power of two.
/// If a shard_amount which is not a power of two is provided, the function will panic.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let mappings = DashMap::with_capacity_and_shard_amount(32, 32);
/// mappings.insert(2, 4);
/// mappings.insert(8, 16);
/// ```
pub fn with_capacity_and_shard_amount(capacity: usize, shard_amount: usize) -> Self {
Self::with_capacity_and_hasher_and_shard_amount(
capacity,
RandomState::default(),
shard_amount,
)
}
}
impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> DashMap<K, V, S> {
/// Wraps this `DashMap` into a read-only view. This view allows to obtain raw references to the stored values.
pub fn into_read_only(self) -> ReadOnlyView<K, V, S> {
ReadOnlyView::new(self)
}
/// Creates a new DashMap with a capacity of 0 and the provided hasher.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
/// use std::collections::hash_map::RandomState;
///
/// let s = RandomState::new();
/// let reviews = DashMap::with_hasher(s);
/// reviews.insert("Veloren", "What a fantastic game!");
/// ```
pub fn with_hasher(hasher: S) -> Self {
Self::with_capacity_and_hasher(0, hasher)
}
/// Creates a new DashMap with a specified starting capacity and hasher.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
/// use std::collections::hash_map::RandomState;
///
/// let s = RandomState::new();
/// let mappings = DashMap::with_capacity_and_hasher(2, s);
/// mappings.insert(2, 4);
/// mappings.insert(8, 16);
/// ```
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {
Self::with_capacity_and_hasher_and_shard_amount(capacity, hasher, default_shard_amount())
}
/// Creates a new DashMap with a specified hasher and shard amount
///
/// shard_amount should be greater than 0 and a power of two.
/// If a shard_amount which is not a power of two is provided, the function will panic.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
/// use std::collections::hash_map::RandomState;
///
/// let s = RandomState::new();
/// let mappings = DashMap::with_hasher_and_shard_amount(s, 32);
/// mappings.insert(2, 4);
/// mappings.insert(8, 16);
/// ```
pub fn with_hasher_and_shard_amount(hasher: S, shard_amount: usize) -> Self {
Self::with_capacity_and_hasher_and_shard_amount(0, hasher, shard_amount)
}
/// Creates a new DashMap with a specified starting capacity, hasher and shard_amount.
///
/// shard_amount should greater than 0 and be a power of two.
/// If a shard_amount which is not a power of two is provided, the function will panic.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
/// use std::collections::hash_map::RandomState;
///
/// let s = RandomState::new();
/// let mappings = DashMap::with_capacity_and_hasher_and_shard_amount(2, s, 32);
/// mappings.insert(2, 4);
/// mappings.insert(8, 16);
/// ```
pub fn with_capacity_and_hasher_and_shard_amount(
mut capacity: usize,
hasher: S,
shard_amount: usize,
) -> Self {
assert!(shard_amount > 1);
assert!(shard_amount.is_power_of_two());
let shift = util::ptr_size_bits() - ncb(shard_amount);
if capacity != 0 {
capacity = (capacity + (shard_amount - 1)) & !(shard_amount - 1);
}
let cps = capacity / shard_amount;
let shards = (0..shard_amount)
.map(|_| RwLock::new(HashMap::with_capacity_and_hasher(cps, hasher.clone())))
.collect();
Self {
shift,
shards,
hasher,
}
}
/// Hash a given item to produce a usize.
/// Uses the provided or default HashBuilder.
pub fn hash_usize<T: Hash>(&self, item: &T) -> usize {
let mut hasher = self.hasher.build_hasher();
item.hash(&mut hasher);
hasher.finish() as usize
}
cfg_if! {
if #[cfg(feature = "raw-api")] {
/// Allows you to peek at the inner shards that store your data.
/// You should probably not use this unless you know what you are doing.
///
/// Requires the `raw-api` feature to be enabled.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let map = DashMap::<(), ()>::new();
/// println!("Amount of shards: {}", map.shards().len());
/// ```
pub fn shards(&self) -> &[RwLock<HashMap<K, V, S>>] {
&self.shards
}
/// Provides mutable access to the inner shards that store your data.
/// You should probably not use this unless you know what you are doing.
///
/// Requires the `raw-api` feature to be enabled.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
/// use dashmap::SharedValue;
///
/// let mut map = DashMap::<i32, &'static str>::new();
/// let shard_ind = map.determine_map(&42);
/// map.shards_mut()[shard_ind].get_mut().insert(42, SharedValue::new("forty two"));
/// assert_eq!(*map.get(&42).unwrap(), "forty two");
/// ```
pub fn shards_mut(&mut self) -> &mut [RwLock<HashMap<K, V, S>>] {
&mut self.shards
}
/// Consumes this `DashMap` and returns the inner shards.
/// You should probably not use this unless you know what you are doing.
///
/// Requires the `raw-api` feature to be enabled.
///
/// See [`DashMap::shards()`] and [`DashMap::shards_mut()`] for more information.
pub fn into_shards(self) -> Box<[RwLock<HashMap<K, V, S>>]> {
self.shards
}
} else {
#[allow(dead_code)]
pub(crate) fn shards(&self) -> &[RwLock<HashMap<K, V, S>>] {
&self.shards
}
#[allow(dead_code)]
pub(crate) fn shards_mut(&mut self) -> &mut [RwLock<HashMap<K, V, S>>] {
&mut self.shards
}
#[allow(dead_code)]
pub(crate) fn into_shards(self) -> Box<[RwLock<HashMap<K, V, S>>]> {
self.shards
}
}
}
cfg_if! {
if #[cfg(feature = "raw-api")] {
/// Finds which shard a certain key is stored in.
/// You should probably not use this unless you know what you are doing.
/// Note that shard selection is dependent on the default or provided HashBuilder.
///
/// Requires the `raw-api` feature to be enabled.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let map = DashMap::new();
/// map.insert("coca-cola", 1.4);
/// println!("coca-cola is stored in shard: {}", map.determine_map("coca-cola"));
/// ```
pub fn determine_map<Q>(&self, key: &Q) -> usize
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
self.determine_shard(hash)
}
}
}
cfg_if! {
if #[cfg(feature = "raw-api")] {
/// Finds which shard a certain hash is stored in.
///
/// Requires the `raw-api` feature to be enabled.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let map: DashMap<i32, i32> = DashMap::new();
/// let key = "key";
/// let hash = map.hash_usize(&key);
/// println!("hash is stored in shard: {}", map.determine_shard(hash));
/// ```
pub fn determine_shard(&self, hash: usize) -> usize {
// Leave the high 7 bits for the HashBrown SIMD tag.
(hash << 7) >> self.shift
}
} else {
pub(crate) fn determine_shard(&self, hash: usize) -> usize {
// Leave the high 7 bits for the HashBrown SIMD tag.
(hash << 7) >> self.shift
}
}
}
/// Returns a reference to the map's [`BuildHasher`].
///
/// # Examples
///
/// ```rust
/// use dashmap::DashMap;
/// use std::collections::hash_map::RandomState;
///
/// let hasher = RandomState::new();
/// let map: DashMap<i32, i32> = DashMap::new();
/// let hasher: &RandomState = map.hasher();
/// ```
///
/// [`BuildHasher`]: https://doc.rust-lang.org/std/hash/trait.BuildHasher.html
pub fn hasher(&self) -> &S {
&self.hasher
}
/// Inserts a key and a value into the map. Returns the old value associated with the key if there was one.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let map = DashMap::new();
/// map.insert("I am the key!", "And I am the value!");
/// ```
pub fn insert(&self, key: K, value: V) -> Option<V> {
self._insert(key, value)
}
/// Removes an entry from the map, returning the key and value if they existed in the map.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let soccer_team = DashMap::new();
/// soccer_team.insert("Jack", "Goalie");
/// assert_eq!(soccer_team.remove("Jack").unwrap().1, "Goalie");
/// ```
pub fn remove<Q>(&self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._remove(key)
}
/// Removes an entry from the map, returning the key and value
/// if the entry existed and the provided conditional function returned true.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
///
/// ```
/// use dashmap::DashMap;
///
/// let soccer_team = DashMap::new();
/// soccer_team.insert("Sam", "Forward");
/// soccer_team.remove_if("Sam", |_, position| position == &"Goalie");
/// assert!(soccer_team.contains_key("Sam"));
/// ```
/// ```
/// use dashmap::DashMap;
///
/// let soccer_team = DashMap::new();
/// soccer_team.insert("Sam", "Forward");
/// soccer_team.remove_if("Sam", |_, position| position == &"Forward");
/// assert!(!soccer_team.contains_key("Sam"));
/// ```
pub fn remove_if<Q>(&self, key: &Q, f: impl FnOnce(&K, &V) -> bool) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._remove_if(key, f)
}
pub fn remove_if_mut<Q>(&self, key: &Q, f: impl FnOnce(&K, &mut V) -> bool) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._remove_if_mut(key, f)
}
/// Creates an iterator over a DashMap yielding immutable references.
///
/// **Locking behaviour:** May deadlock if called when holding a mutable reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let words = DashMap::new();
/// words.insert("hello", "world");
/// assert_eq!(words.iter().count(), 1);
/// ```
pub fn iter(&'a self) -> Iter<'a, K, V, S, DashMap<K, V, S>> {
self._iter()
}
/// Iterator over a DashMap yielding mutable references.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let map = DashMap::new();
/// map.insert("Johnny", 21);
/// map.iter_mut().for_each(|mut r| *r += 1);
/// assert_eq!(*map.get("Johnny").unwrap(), 22);
/// ```
pub fn iter_mut(&'a self) -> IterMut<'a, K, V, S, DashMap<K, V, S>> {
self._iter_mut()
}
/// Get an immutable reference to an entry in the map
///
/// **Locking behaviour:** May deadlock if called when holding a mutable reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let youtubers = DashMap::new();
/// youtubers.insert("Bosnian Bill", 457000);
/// assert_eq!(*youtubers.get("Bosnian Bill").unwrap(), 457000);
/// ```
pub fn get<Q>(&'a self, key: &Q) -> Option<Ref<'a, K, V, S>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._get(key)
}
/// Get a mutable reference to an entry in the map
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let class = DashMap::new();
/// class.insert("Albin", 15);
/// *class.get_mut("Albin").unwrap() -= 1;
/// assert_eq!(*class.get("Albin").unwrap(), 14);
/// ```
pub fn get_mut<Q>(&'a self, key: &Q) -> Option<RefMut<'a, K, V, S>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._get_mut(key)
}
/// Get an immutable reference to an entry in the map, if the shard is not locked.
/// If the shard is locked, the function will return [TryResult::Locked].
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
/// use dashmap::try_result::TryResult;
///
/// let map = DashMap::new();
/// map.insert("Johnny", 21);
///
/// assert_eq!(*map.try_get("Johnny").unwrap(), 21);
///
/// let _result1_locking = map.get_mut("Johnny");
///
/// let result2 = map.try_get("Johnny");
/// assert!(result2.is_locked());
/// ```
pub fn try_get<Q>(&'a self, key: &Q) -> TryResult<Ref<'a, K, V, S>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._try_get(key)
}
/// Get a mutable reference to an entry in the map, if the shard is not locked.
/// If the shard is locked, the function will return [TryResult::Locked].
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
/// use dashmap::try_result::TryResult;
///
/// let map = DashMap::new();
/// map.insert("Johnny", 21);
///
/// *map.try_get_mut("Johnny").unwrap() += 1;
/// assert_eq!(*map.get("Johnny").unwrap(), 22);
///
/// let _result1_locking = map.get("Johnny");
///
/// let result2 = map.try_get_mut("Johnny");
/// assert!(result2.is_locked());
/// ```
pub fn try_get_mut<Q>(&'a self, key: &Q) -> TryResult<RefMut<'a, K, V, S>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._try_get_mut(key)
}
/// Remove excess capacity to reduce memory usage.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
pub fn shrink_to_fit(&self) {
self._shrink_to_fit();
}
/// Retain elements that whose predicates return true
/// and discard elements whose predicates return false.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let people = DashMap::new();
/// people.insert("Albin", 15);
/// people.insert("Jones", 22);
/// people.insert("Charlie", 27);
/// people.retain(|_, v| *v > 20);
/// assert_eq!(people.len(), 2);
/// ```
pub fn retain(&self, f: impl FnMut(&K, &mut V) -> bool) {
self._retain(f);
}
/// Fetches the total number of key-value pairs stored in the map.
///
/// **Locking behaviour:** May deadlock if called when holding a mutable reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let people = DashMap::new();
/// people.insert("Albin", 15);
/// people.insert("Jones", 22);
/// people.insert("Charlie", 27);
/// assert_eq!(people.len(), 3);
/// ```
pub fn len(&self) -> usize {
self._len()
}
/// Checks if the map is empty or not.
///
/// **Locking behaviour:** May deadlock if called when holding a mutable reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let map = DashMap::<(), ()>::new();
/// assert!(map.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self._is_empty()
}
/// Removes all key-value pairs in the map.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let stats = DashMap::new();
/// stats.insert("Goals", 4);
/// assert!(!stats.is_empty());
/// stats.clear();
/// assert!(stats.is_empty());
/// ```
pub fn clear(&self) {
self._clear();
}
/// Returns how many key-value pairs the map can store without reallocating.
///
/// **Locking behaviour:** May deadlock if called when holding a mutable reference into the map.
pub fn capacity(&self) -> usize {
self._capacity()
}
/// Modify a specific value according to a function.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let stats = DashMap::new();
/// stats.insert("Goals", 4);
/// stats.alter("Goals", |_, v| v * 2);
/// assert_eq!(*stats.get("Goals").unwrap(), 8);
/// ```
///
/// # Panics
///
/// If the given closure panics, then `alter` will abort the process
pub fn alter<Q>(&self, key: &Q, f: impl FnOnce(&K, V) -> V)
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._alter(key, f);
}
/// Modify every value in the map according to a function.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let stats = DashMap::new();
/// stats.insert("Wins", 4);
/// stats.insert("Losses", 2);
/// stats.alter_all(|_, v| v + 1);
/// assert_eq!(*stats.get("Wins").unwrap(), 5);
/// assert_eq!(*stats.get("Losses").unwrap(), 3);
/// ```
///
/// # Panics
///
/// If the given closure panics, then `alter_all` will abort the process
pub fn alter_all(&self, f: impl FnMut(&K, V) -> V) {
self._alter_all(f);
}
/// Scoped access into an item of the map according to a function.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let warehouse = DashMap::new();
/// warehouse.insert(4267, ("Banana", 100));
/// warehouse.insert(2359, ("Pear", 120));
/// let fruit = warehouse.view(&4267, |_k, v| *v);
/// assert_eq!(fruit, Some(("Banana", 100)));
/// ```
///
/// # Panics
///
/// If the given closure panics, then `view` will abort the process
pub fn view<Q, R>(&self, key: &Q, f: impl FnOnce(&K, &V) -> R) -> Option<R>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._view(key, f)
}
/// Checks if the map contains a specific key.
///
/// **Locking behaviour:** May deadlock if called when holding a mutable reference into the map.
///
/// # Examples
///
/// ```
/// use dashmap::DashMap;
///
/// let team_sizes = DashMap::new();
/// team_sizes.insert("Dakota Cherries", 23);
/// assert!(team_sizes.contains_key("Dakota Cherries"));
/// ```
pub fn contains_key<Q>(&self, key: &Q) -> bool
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self._contains_key(key)
}
/// Advanced entry API that tries to mimic `std::collections::HashMap`.
/// See the documentation on `dashmap::mapref::entry` for more details.
///
/// **Locking behaviour:** May deadlock if called when holding any sort of reference into the map.
pub fn entry(&'a self, key: K) -> Entry<'a, K, V, S> {
self._entry(key)
}
/// Advanced entry API that tries to mimic `std::collections::HashMap`.
/// See the documentation on `dashmap::mapref::entry` for more details.
///
/// Returns None if the shard is currently locked.
pub fn try_entry(&'a self, key: K) -> Option<Entry<'a, K, V, S>> {
self._try_entry(key)
}
/// Advanced entry API that tries to mimic `std::collections::HashMap::try_reserve`.
/// Tries to reserve capacity for at least `shard * additional`
/// and may reserve more space to avoid frequent reallocations.
///
/// # Errors
///
/// If the capacity overflows, or the allocator reports a failure, then an error is returned.
// TODO: return std::collections::TryReserveError once std::collections::TryReserveErrorKind stabilises.
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
for shard in self.shards.iter() {
shard
.write()
.try_reserve(additional)
.map_err(|_| TryReserveError {})?;
}
Ok(())
}
}
impl<'a, K: 'a + Eq + Hash, V: 'a, S: 'a + BuildHasher + Clone> Map<'a, K, V, S>
for DashMap<K, V, S>
{
fn _shard_count(&self) -> usize {
self.shards.len()
}
unsafe fn _get_read_shard(&'a self, i: usize) -> &'a HashMap<K, V, S> {
debug_assert!(i < self.shards.len());
&*self.shards.get_unchecked(i).data_ptr()
}
unsafe fn _yield_read_shard(&'a self, i: usize) -> RwLockReadGuard<'a, HashMap<K, V, S>> {
debug_assert!(i < self.shards.len());
self.shards.get_unchecked(i).read()
}
unsafe fn _yield_write_shard(&'a self, i: usize) -> RwLockWriteGuard<'a, HashMap<K, V, S>> {
debug_assert!(i < self.shards.len());
self.shards.get_unchecked(i).write()
}
unsafe fn _try_yield_read_shard(
&'a self,
i: usize,
) -> Option<RwLockReadGuard<'a, HashMap<K, V, S>>> {
debug_assert!(i < self.shards.len());
self.shards.get_unchecked(i).try_read()
}
unsafe fn _try_yield_write_shard(
&'a self,
i: usize,
) -> Option<RwLockWriteGuard<'a, HashMap<K, V, S>>> {
debug_assert!(i < self.shards.len());
self.shards.get_unchecked(i).try_write()
}
fn _insert(&self, key: K, value: V) -> Option<V> {
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let mut shard = unsafe { self._yield_write_shard(idx) };
shard
.insert(key, SharedValue::new(value))
.map(|v| v.into_inner())
}
fn _remove<Q>(&self, key: &Q) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let mut shard = unsafe { self._yield_write_shard(idx) };
shard.remove_entry(key).map(|(k, v)| (k, v.into_inner()))
}
fn _remove_if<Q>(&self, key: &Q, f: impl FnOnce(&K, &V) -> bool) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let mut shard = unsafe { self._yield_write_shard(idx) };
if let Some((kptr, vptr)) = shard.get_key_value(key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();
if f(&*kptr, &mut *vptr) {
shard.remove_entry(key).map(|(k, v)| (k, v.into_inner()))
} else {
None
}
}
} else {
None
}
}
fn _remove_if_mut<Q>(&self, key: &Q, f: impl FnOnce(&K, &mut V) -> bool) -> Option<(K, V)>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let mut shard = unsafe { self._yield_write_shard(idx) };
if let Some((kptr, vptr)) = shard.get_key_value(key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();
if f(&*kptr, &mut *vptr) {
shard.remove_entry(key).map(|(k, v)| (k, v.into_inner()))
} else {
None
}
}
} else {
None
}
}
fn _iter(&'a self) -> Iter<'a, K, V, S, DashMap<K, V, S>> {
Iter::new(self)
}
fn _iter_mut(&'a self) -> IterMut<'a, K, V, S, DashMap<K, V, S>> {
IterMut::new(self)
}
fn _get<Q>(&'a self, key: &Q) -> Option<Ref<'a, K, V, S>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let shard = unsafe { self._yield_read_shard(idx) };
if let Some((kptr, vptr)) = shard.get_key_value(key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *const V = vptr.get();
Some(Ref::new(shard, kptr, vptr))
}
} else {
None
}
}
fn _get_mut<Q>(&'a self, key: &Q) -> Option<RefMut<'a, K, V, S>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let shard = unsafe { self._yield_write_shard(idx) };
if let Some((kptr, vptr)) = shard.get_key_value(key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();
Some(RefMut::new(shard, kptr, vptr))
}
} else {
None
}
}
fn _try_get<Q>(&'a self, key: &Q) -> TryResult<Ref<'a, K, V, S>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let shard = match unsafe { self._try_yield_read_shard(idx) } {
Some(shard) => shard,
None => return TryResult::Locked,
};
if let Some((kptr, vptr)) = shard.get_key_value(key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *const V = vptr.get();
TryResult::Present(Ref::new(shard, kptr, vptr))
}
} else {
TryResult::Absent
}
}
fn _try_get_mut<Q>(&'a self, key: &Q) -> TryResult<RefMut<'a, K, V, S>>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let shard = match unsafe { self._try_yield_write_shard(idx) } {
Some(shard) => shard,
None => return TryResult::Locked,
};
if let Some((kptr, vptr)) = shard.get_key_value(key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();
TryResult::Present(RefMut::new(shard, kptr, vptr))
}
} else {
TryResult::Absent
}
}
fn _shrink_to_fit(&self) {
self.shards.iter().for_each(|s| s.write().shrink_to_fit());
}
fn _retain(&self, mut f: impl FnMut(&K, &mut V) -> bool) {
self.shards
.iter()
.for_each(|s| s.write().retain(|k, v| f(k, v.get_mut())));
}
fn _len(&self) -> usize {
self.shards.iter().map(|s| s.read().len()).sum()
}
fn _capacity(&self) -> usize {
self.shards.iter().map(|s| s.read().capacity()).sum()
}
fn _alter<Q>(&self, key: &Q, f: impl FnOnce(&K, V) -> V)
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
if let Some(mut r) = self.get_mut(key) {
util::map_in_place_2(r.pair_mut(), f);
}
}
fn _alter_all(&self, mut f: impl FnMut(&K, V) -> V) {
self.shards.iter().for_each(|s| {
s.write()
.iter_mut()
.for_each(|(k, v)| util::map_in_place_2((k, v.get_mut()), &mut f));
});
}
fn _view<Q, R>(&self, key: &Q, f: impl FnOnce(&K, &V) -> R) -> Option<R>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
self.get(key).map(|r| {
let (k, v) = r.pair();
f(k, v)
})
}
fn _entry(&'a self, key: K) -> Entry<'a, K, V, S> {
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let shard = unsafe { self._yield_write_shard(idx) };
if let Some((kptr, vptr)) = shard.get_key_value(&key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();
Entry::Occupied(OccupiedEntry::new(shard, key, (kptr, vptr)))
}
} else {
unsafe { Entry::Vacant(VacantEntry::new(shard, key)) }
}
}
fn _try_entry(&'a self, key: K) -> Option<Entry<'a, K, V, S>> {
let hash = self.hash_usize(&key);
let idx = self.determine_shard(hash);
let shard = match unsafe { self._try_yield_write_shard(idx) } {
Some(shard) => shard,
None => return None,
};
if let Some((kptr, vptr)) = shard.get_key_value(&key) {
unsafe {
let kptr: *const K = kptr;
let vptr: *mut V = vptr.as_ptr();
Some(Entry::Occupied(OccupiedEntry::new(
shard,
key,
(kptr, vptr),
)))
}
} else {
unsafe { Some(Entry::Vacant(VacantEntry::new(shard, key))) }
}
}
fn _hasher(&self) -> S {
self.hasher.clone()
}
}
impl<K: Eq + Hash + fmt::Debug, V: fmt::Debug, S: BuildHasher + Clone> fmt::Debug
for DashMap<K, V, S>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut pmap = f.debug_map();
for r in self {
let (k, v) = r.pair();
pmap.entry(k, v);
}
pmap.finish()
}
}
impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone> Shl<(K, V)> for &'a DashMap<K, V, S> {
type Output = Option<V>;
fn shl(self, pair: (K, V)) -> Self::Output {
self.insert(pair.0, pair.1)
}
}
impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone, Q> Shr<&Q> for &'a DashMap<K, V, S>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
type Output = Ref<'a, K, V, S>;
fn shr(self, key: &Q) -> Self::Output {
self.get(key).unwrap()
}
}
impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone, Q> BitOr<&Q> for &'a DashMap<K, V, S>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
type Output = RefMut<'a, K, V, S>;
fn bitor(self, key: &Q) -> Self::Output {
self.get_mut(key).unwrap()
}
}
impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone, Q> Sub<&Q> for &'a DashMap<K, V, S>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
type Output = Option<(K, V)>;
fn sub(self, key: &Q) -> Self::Output {
self.remove(key)
}
}
impl<'a, K: 'a + Eq + Hash, V: 'a, S: BuildHasher + Clone, Q> BitAnd<&Q> for &'a DashMap<K, V, S>
where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
type Output = bool;
fn bitand(self, key: &Q) -> Self::Output {
self.contains_key(key)
}
}
impl<K: Eq + Hash, V, S: BuildHasher + Clone> IntoIterator for DashMap<K, V, S> {
type Item = (K, V);
type IntoIter = OwningIter<K, V, S>;
fn into_iter(self) -> Self::IntoIter {
OwningIter::new(self)
}
}
impl<'a, K: Eq + Hash, V, S: BuildHasher + Clone> IntoIterator for &'a DashMap<K, V, S> {
type Item = RefMulti<'a, K, V, S>;
type IntoIter = Iter<'a, K, V, S, DashMap<K, V, S>>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl<K: Eq + Hash, V, S: BuildHasher + Clone> Extend<(K, V)> for DashMap<K, V, S> {
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, intoiter: I) {
for pair in intoiter.into_iter() {
self.insert(pair.0, pair.1);
}
}
}
impl<K: Eq + Hash, V, S: BuildHasher + Clone + Default> FromIterator<(K, V)> for DashMap<K, V, S> {
fn from_iter<I: IntoIterator<Item = (K, V)>>(intoiter: I) -> Self {
let mut map = DashMap::default();
map.extend(intoiter);
map
}
}
#[cfg(test)]
mod tests {
use crate::DashMap;
use std::collections::hash_map::RandomState;
#[test]
fn test_basic() {
let dm = DashMap::new();
dm.insert(0, 0);
assert_eq!(dm.get(&0).unwrap().value(), &0);
}
#[test]
fn test_default() {
let dm: DashMap<u32, u32> = DashMap::default();
dm.insert(0, 0);
assert_eq!(dm.get(&0).unwrap().value(), &0);
}
#[test]
fn test_multiple_hashes() {
let dm: DashMap<u32, u32> = DashMap::default();
for i in 0..100 {
dm.insert(0, i);
dm.insert(i, i);
}
for i in 1..100 {
let r = dm.get(&i).unwrap();
assert_eq!(i, *r.value());
assert_eq!(i, *r.key());
}
let r = dm.get(&0).unwrap();
assert_eq!(99, *r.value());
}
#[test]
fn test_more_complex_values() {
#[derive(Hash, PartialEq, Debug, Clone)]
struct T0 {
s: String,
u: u8,
}
let dm = DashMap::new();
let range = 0..10;
for i in range {
let t = T0 {
s: i.to_string(),
u: i as u8,
};
dm.insert(i, t.clone());
assert_eq!(&t, dm.get(&i).unwrap().value());
}
}
#[test]
fn test_different_hashers_randomstate() {
let dm_hm_default: DashMap<u32, u32, RandomState> =
DashMap::with_hasher(RandomState::new());
for i in 0..10 {
dm_hm_default.insert(i, i);
assert_eq!(i, *dm_hm_default.get(&i).unwrap().value());
}
}
#[test]
fn test_map_view() {
let dm = DashMap::new();
let vegetables: [String; 4] = [
"Salad".to_string(),
"Beans".to_string(),
"Potato".to_string(),
"Tomato".to_string(),
];
// Give it some values
dm.insert(0, "Banana".to_string());
dm.insert(4, "Pear".to_string());
dm.insert(9, "Potato".to_string());
dm.insert(12, "Chicken".to_string());
let potato_vegetableness = dm.view(&9, |_, v| vegetables.contains(v));
assert_eq!(potato_vegetableness, Some(true));
let chicken_vegetableness = dm.view(&12, |_, v| vegetables.contains(v));
assert_eq!(chicken_vegetableness, Some(false));
let not_in_map = dm.view(&30, |_k, _v| false);
assert_eq!(not_in_map, None);
}
#[test]
fn test_try_get() {
{
let map = DashMap::new();
map.insert("Johnny", 21);
assert_eq!(*map.try_get("Johnny").unwrap(), 21);
let _result1_locking = map.get_mut("Johnny");
let result2 = map.try_get("Johnny");
assert!(result2.is_locked());
}
{
let map = DashMap::new();
map.insert("Johnny", 21);
*map.try_get_mut("Johnny").unwrap() += 1;
assert_eq!(*map.get("Johnny").unwrap(), 22);
let _result1_locking = map.get("Johnny");
let result2 = map.try_get_mut("Johnny");
assert!(result2.is_locked());
}
}
#[test]
fn test_try_reserve() {
let mut map: DashMap<i32, i32> = DashMap::new();
// DashMap is empty and doesn't allocate memory
assert_eq!(map.capacity(), 0);
map.try_reserve(10).unwrap();
// And now map can hold at least 10 elements
assert!(map.capacity() >= 10);
}
#[test]
fn test_try_reserve_errors() {
let mut map: DashMap<i32, i32> = DashMap::new();
match map.try_reserve(usize::MAX) {
Err(_) => {}
_ => panic!("should have raised CapacityOverflow error"),
}
}
}