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