@@ -1470,6 +1470,10 @@ where
1470
1470
/// mutable reference will be returned to any value. `None` will be returned if any of the
1471
1471
/// keys are duplicates or missing.
1472
1472
///
1473
+ /// # Panics
1474
+ ///
1475
+ /// Panics if some keys are overlapping.
1476
+ ///
1473
1477
/// # Examples
1474
1478
///
1475
1479
/// ```
@@ -1487,27 +1491,33 @@ where
1487
1491
/// ]);
1488
1492
/// assert_eq!(
1489
1493
/// got,
1490
- /// Some( [
1491
- /// &mut 1807,
1492
- /// &mut 1800,
1493
- /// ]) ,
1494
+ /// [
1495
+ /// Some( &mut 1807) ,
1496
+ /// Some( &mut 1800) ,
1497
+ /// ],
1494
1498
/// );
1495
1499
///
1496
1500
/// // Missing keys result in None
1497
1501
/// let got = libraries.get_many_mut([
1498
- /// "Athenæum ",
1502
+ /// "Athenæum1 ",
1499
1503
/// "New York Public Library",
1500
1504
/// ]);
1501
- /// assert_eq!(got, None);
1505
+ /// assert_eq!(got, [None, None]);
1506
+ /// ```
1507
+ ///
1508
+ /// ```should_panic
1509
+ /// use hashbrown::HashMap;
1510
+ ///
1511
+ /// let mut libraries = HashMap::new();
1512
+ /// libraries.insert("Athenæum".to_string(), 1807);
1502
1513
///
1503
- /// // Duplicate keys result in None
1514
+ /// // Duplicate keys panic!
1504
1515
/// let got = libraries.get_many_mut([
1505
1516
/// "Athenæum",
1506
1517
/// "Athenæum",
1507
1518
/// ]);
1508
- /// assert_eq!(got, None);
1509
1519
/// ```
1510
- pub fn get_many_mut < Q , const N : usize > ( & mut self , ks : [ & Q ; N ] ) -> Option < [ & ' _ mut V ; N ] >
1520
+ pub fn get_many_mut < Q , const N : usize > ( & mut self , ks : [ & Q ; N ] ) -> [ Option < & ' _ mut V > ; N ]
1511
1521
where
1512
1522
Q : Hash + Equivalent < K > + ?Sized ,
1513
1523
{
@@ -1517,8 +1527,8 @@ where
1517
1527
/// Attempts to get mutable references to `N` values in the map at once, without validating that
1518
1528
/// the values are unique.
1519
1529
///
1520
- /// Returns an array of length `N` with the results of each query. `None` will be returned if
1521
- /// any of the keys are missing.
1530
+ /// Returns an array of length `N` with the results of each query. `None` will be used if
1531
+ /// the key is missing.
1522
1532
///
1523
1533
/// For a safe alternative see [`get_many_mut`](`HashMap::get_many_mut`).
1524
1534
///
@@ -1540,29 +1550,31 @@ where
1540
1550
/// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691);
1541
1551
/// libraries.insert("Library of Congress".to_string(), 1800);
1542
1552
///
1543
- /// let got = libraries.get_many_mut([
1553
+ /// // SAFETY: The keys do not overlap.
1554
+ /// let got = unsafe { libraries.get_many_unchecked_mut([
1544
1555
/// "Athenæum",
1545
1556
/// "Library of Congress",
1546
- /// ]);
1557
+ /// ]) } ;
1547
1558
/// assert_eq!(
1548
1559
/// got,
1549
- /// Some( [
1550
- /// &mut 1807,
1551
- /// &mut 1800,
1552
- /// ]) ,
1560
+ /// [
1561
+ /// Some( &mut 1807) ,
1562
+ /// Some( &mut 1800) ,
1563
+ /// ],
1553
1564
/// );
1554
1565
///
1555
- /// // Missing keys result in None
1556
- /// let got = libraries.get_many_mut ([
1566
+ /// // SAFETY: The keys do not overlap.
1567
+ /// let got = unsafe { libraries.get_many_unchecked_mut ([
1557
1568
/// "Athenæum",
1558
1569
/// "New York Public Library",
1559
- /// ]);
1560
- /// assert_eq!(got, None);
1570
+ /// ]) };
1571
+ /// // Missing keys result in None
1572
+ /// assert_eq!(got, [Some(&mut 1807), None]);
1561
1573
/// ```
1562
1574
pub unsafe fn get_many_unchecked_mut < Q , const N : usize > (
1563
1575
& mut self ,
1564
1576
ks : [ & Q ; N ] ,
1565
- ) -> Option < [ & ' _ mut V ; N ] >
1577
+ ) -> [ Option < & ' _ mut V > ; N ]
1566
1578
where
1567
1579
Q : Hash + Equivalent < K > + ?Sized ,
1568
1580
{
@@ -1574,8 +1586,11 @@ where
1574
1586
/// references to the corresponding keys.
1575
1587
///
1576
1588
/// Returns an array of length `N` with the results of each query. For soundness, at most one
1577
- /// mutable reference will be returned to any value. `None` will be returned if any of the keys
1578
- /// are duplicates or missing.
1589
+ /// mutable reference will be returned to any value. `None` will be used if the key is missing.
1590
+ ///
1591
+ /// # Panics
1592
+ ///
1593
+ /// Panics if some keys are overlapping.
1579
1594
///
1580
1595
/// # Examples
1581
1596
///
@@ -1594,30 +1609,37 @@ where
1594
1609
/// ]);
1595
1610
/// assert_eq!(
1596
1611
/// got,
1597
- /// Some( [
1598
- /// ( &"Bodleian Library".to_string(), &mut 1602),
1599
- /// ( &"Herzogin-Anna-Amalia-Bibliothek".to_string(), &mut 1691),
1600
- /// ]) ,
1612
+ /// [
1613
+ /// Some(( &"Bodleian Library".to_string(), &mut 1602) ),
1614
+ /// Some(( &"Herzogin-Anna-Amalia-Bibliothek".to_string(), &mut 1691) ),
1615
+ /// ],
1601
1616
/// );
1602
1617
/// // Missing keys result in None
1603
1618
/// let got = libraries.get_many_key_value_mut([
1604
1619
/// "Bodleian Library",
1605
1620
/// "Gewandhaus",
1606
1621
/// ]);
1607
- /// assert_eq!(got, None);
1622
+ /// assert_eq!(got, [Some((&"Bodleian Library".to_string(), &mut 1602)), None]);
1623
+ /// ```
1624
+ ///
1625
+ /// ```should_panic
1626
+ /// use hashbrown::HashMap;
1627
+ ///
1628
+ /// let mut libraries = HashMap::new();
1629
+ /// libraries.insert("Bodleian Library".to_string(), 1602);
1630
+ /// libraries.insert("Herzogin-Anna-Amalia-Bibliothek".to_string(), 1691);
1608
1631
///
1609
- /// // Duplicate keys result in None
1632
+ /// // Duplicate keys result in panic!
1610
1633
/// let got = libraries.get_many_key_value_mut([
1611
1634
/// "Bodleian Library",
1612
1635
/// "Herzogin-Anna-Amalia-Bibliothek",
1613
1636
/// "Herzogin-Anna-Amalia-Bibliothek",
1614
1637
/// ]);
1615
- /// assert_eq!(got, None);
1616
1638
/// ```
1617
1639
pub fn get_many_key_value_mut < Q , const N : usize > (
1618
1640
& mut self ,
1619
1641
ks : [ & Q ; N ] ,
1620
- ) -> Option < [ ( & ' _ K , & ' _ mut V ) ; N ] >
1642
+ ) -> [ Option < ( & ' _ K , & ' _ mut V ) > ; N ]
1621
1643
where
1622
1644
Q : Hash + Equivalent < K > + ?Sized ,
1623
1645
{
@@ -1657,30 +1679,36 @@ where
1657
1679
/// ]);
1658
1680
/// assert_eq!(
1659
1681
/// got,
1660
- /// Some( [
1661
- /// ( &"Bodleian Library".to_string(), &mut 1602),
1662
- /// ( &"Herzogin-Anna-Amalia-Bibliothek".to_string(), &mut 1691),
1663
- /// ]) ,
1682
+ /// [
1683
+ /// Some(( &"Bodleian Library".to_string(), &mut 1602) ),
1684
+ /// Some(( &"Herzogin-Anna-Amalia-Bibliothek".to_string(), &mut 1691) ),
1685
+ /// ],
1664
1686
/// );
1665
1687
/// // Missing keys result in None
1666
1688
/// let got = libraries.get_many_key_value_mut([
1667
1689
/// "Bodleian Library",
1668
1690
/// "Gewandhaus",
1669
1691
/// ]);
1670
- /// assert_eq!(got, None);
1692
+ /// assert_eq!(
1693
+ /// got,
1694
+ /// [
1695
+ /// Some((&"Bodleian Library".to_string(), &mut 1602)),
1696
+ /// None,
1697
+ /// ],
1698
+ /// );
1671
1699
/// ```
1672
1700
pub unsafe fn get_many_key_value_unchecked_mut < Q , const N : usize > (
1673
1701
& mut self ,
1674
1702
ks : [ & Q ; N ] ,
1675
- ) -> Option < [ ( & ' _ K , & ' _ mut V ) ; N ] >
1703
+ ) -> [ Option < ( & ' _ K , & ' _ mut V ) > ; N ]
1676
1704
where
1677
1705
Q : Hash + Equivalent < K > + ?Sized ,
1678
1706
{
1679
1707
self . get_many_unchecked_mut_inner ( ks)
1680
1708
. map ( |res| res. map ( |( k, v) | ( & * k, v) ) )
1681
1709
}
1682
1710
1683
- fn get_many_mut_inner < Q , const N : usize > ( & mut self , ks : [ & Q ; N ] ) -> Option < [ & ' _ mut ( K , V ) ; N ] >
1711
+ fn get_many_mut_inner < Q , const N : usize > ( & mut self , ks : [ & Q ; N ] ) -> [ Option < & ' _ mut ( K , V ) > ; N ]
1684
1712
where
1685
1713
Q : Hash + Equivalent < K > + ?Sized ,
1686
1714
{
@@ -1692,7 +1720,7 @@ where
1692
1720
unsafe fn get_many_unchecked_mut_inner < Q , const N : usize > (
1693
1721
& mut self ,
1694
1722
ks : [ & Q ; N ] ,
1695
- ) -> Option < [ & ' _ mut ( K , V ) ; N ] >
1723
+ ) -> [ Option < & ' _ mut ( K , V ) > ; N ]
1696
1724
where
1697
1725
Q : Hash + Equivalent < K > + ?Sized ,
1698
1726
{
@@ -5937,33 +5965,39 @@ mod test_map {
5937
5965
}
5938
5966
5939
5967
#[ test]
5940
- fn test_get_each_mut ( ) {
5968
+ fn test_get_many_mut ( ) {
5941
5969
let mut map = HashMap :: new ( ) ;
5942
5970
map. insert ( "foo" . to_owned ( ) , 0 ) ;
5943
5971
map. insert ( "bar" . to_owned ( ) , 10 ) ;
5944
5972
map. insert ( "baz" . to_owned ( ) , 20 ) ;
5945
5973
map. insert ( "qux" . to_owned ( ) , 30 ) ;
5946
5974
5947
5975
let xs = map. get_many_mut ( [ "foo" , "qux" ] ) ;
5948
- assert_eq ! ( xs, Some ( [ & mut 0 , & mut 30 ] ) ) ;
5976
+ assert_eq ! ( xs, [ Some ( & mut 0 ) , Some ( & mut 30 ) ] ) ;
5949
5977
5950
5978
let xs = map. get_many_mut ( [ "foo" , "dud" ] ) ;
5951
- assert_eq ! ( xs, None ) ;
5952
-
5953
- let xs = map. get_many_mut ( [ "foo" , "foo" ] ) ;
5954
- assert_eq ! ( xs, None ) ;
5979
+ assert_eq ! ( xs, [ Some ( & mut 0 ) , None ] ) ;
5955
5980
5956
5981
let ys = map. get_many_key_value_mut ( [ "bar" , "baz" ] ) ;
5957
5982
assert_eq ! (
5958
5983
ys,
5959
- Some ( [ ( & "bar" . to_owned( ) , & mut 10 ) , ( & "baz" . to_owned( ) , & mut 20 ) , ] ) ,
5984
+ [
5985
+ Some ( ( & "bar" . to_owned( ) , & mut 10 ) ) ,
5986
+ Some ( ( & "baz" . to_owned( ) , & mut 20 ) )
5987
+ ] ,
5960
5988
) ;
5961
5989
5962
5990
let ys = map. get_many_key_value_mut ( [ "bar" , "dip" ] ) ;
5963
- assert_eq ! ( ys, None ) ;
5991
+ assert_eq ! ( ys, [ Some ( ( & "bar" . to_string( ) , & mut 10 ) ) , None ] ) ;
5992
+ }
5993
+
5994
+ #[ test]
5995
+ #[ should_panic = "duplicate keys found" ]
5996
+ fn test_get_many_mut_duplicate ( ) {
5997
+ let mut map = HashMap :: new ( ) ;
5998
+ map. insert ( "foo" . to_owned ( ) , 0 ) ;
5964
5999
5965
- let ys = map. get_many_key_value_mut ( [ "baz" , "baz" ] ) ;
5966
- assert_eq ! ( ys, None ) ;
6000
+ let _xs = map. get_many_mut ( [ "foo" , "foo" ] ) ;
5967
6001
}
5968
6002
5969
6003
#[ test]
0 commit comments