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