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