1+ use std:: assert_matches:: assert_matches;
12use std:: borrow:: Cow ;
23use std:: cell:: Cell ;
34use std:: collections:: TryReserveErrorKind :: * ;
@@ -1488,34 +1489,32 @@ fn test_try_reserve() {
14881489
14891490 if guards_against_isize {
14901491 // Check isize::MAX + 1 does count as overflow
1491- if let Err ( CapacityOverflow ) =
1492- empty_bytes. try_reserve ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
1493- {
1494- } else {
1495- panic ! ( "isize::MAX + 1 should trigger an overflow!" )
1496- }
1492+ assert_matches ! (
1493+ empty_bytes. try_reserve( MAX_CAP + 1 ) . map_err( |e| e. kind( ) ) ,
1494+ Err ( CapacityOverflow ) ,
1495+ "isize::MAX + 1 should trigger an overflow!"
1496+ ) ;
14971497
14981498 // Check usize::MAX does count as overflow
1499- if let Err ( CapacityOverflow ) = empty_bytes . try_reserve ( MAX_USIZE ) . map_err ( |e| e . kind ( ) )
1500- {
1501- } else {
1502- panic ! ( "usize::MAX should trigger an overflow!" )
1503- }
1499+ assert_matches ! (
1500+ empty_bytes . try_reserve ( MAX_USIZE ) . map_err ( |e| e . kind ( ) ) ,
1501+ Err ( CapacityOverflow ) ,
1502+ "usize::MAX should trigger an overflow!"
1503+ ) ;
15041504 } else {
15051505 // Check isize::MAX + 1 is an OOM
1506- if let Err ( AllocError { .. } ) =
1507- empty_bytes. try_reserve ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
1508- {
1509- } else {
1510- panic ! ( "isize::MAX + 1 should trigger an OOM!" )
1511- }
1506+ assert_matches ! (
1507+ empty_bytes. try_reserve( MAX_CAP + 1 ) . map_err( |e| e. kind( ) ) ,
1508+ Err ( AllocError { .. } ) ,
1509+ "isize::MAX + 1 should trigger an OOM!"
1510+ ) ;
15121511
15131512 // Check usize::MAX is an OOM
1514- if let Err ( AllocError { .. } ) = empty_bytes . try_reserve ( MAX_USIZE ) . map_err ( |e| e . kind ( ) )
1515- {
1516- } else {
1517- panic ! ( "usize::MAX should trigger an OOM!" )
1518- }
1513+ assert_matches ! (
1514+ empty_bytes . try_reserve ( MAX_USIZE ) . map_err ( |e| e . kind ( ) ) ,
1515+ Err ( AllocError { .. } ) ,
1516+ "usize::MAX should trigger an OOM!"
1517+ ) ;
15191518 }
15201519 }
15211520
@@ -1530,23 +1529,24 @@ fn test_try_reserve() {
15301529 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
15311530 }
15321531 if guards_against_isize {
1533- if let Err ( CapacityOverflow ) = ten_bytes . try_reserve ( MAX_CAP - 9 ) . map_err ( |e| e . kind ( ) )
1534- {
1535- } else {
1536- panic ! ( "isize::MAX + 1 should trigger an overflow!" ) ;
1537- }
1532+ assert_matches ! (
1533+ ten_bytes . try_reserve ( MAX_CAP - 9 ) . map_err ( |e| e . kind ( ) ) ,
1534+ Err ( CapacityOverflow ) ,
1535+ "isize::MAX + 1 should trigger an overflow!"
1536+ ) ;
15381537 } else {
1539- if let Err ( AllocError { .. } ) = ten_bytes . try_reserve ( MAX_CAP - 9 ) . map_err ( |e| e . kind ( ) )
1540- {
1541- } else {
1542- panic ! ( "isize::MAX + 1 should trigger an OOM!" )
1543- }
1538+ assert_matches ! (
1539+ ten_bytes . try_reserve ( MAX_CAP - 9 ) . map_err ( |e| e . kind ( ) ) ,
1540+ Err ( AllocError { .. } ) ,
1541+ "isize::MAX + 1 should trigger an OOM!"
1542+ ) ;
15441543 }
15451544 // Should always overflow in the add-to-len
1546- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve ( MAX_USIZE ) . map_err ( |e| e. kind ( ) ) {
1547- } else {
1548- panic ! ( "usize::MAX should trigger an overflow!" )
1549- }
1545+ assert_matches ! (
1546+ ten_bytes. try_reserve( MAX_USIZE ) . map_err( |e| e. kind( ) ) ,
1547+ Err ( CapacityOverflow ) ,
1548+ "usize::MAX should trigger an overflow!"
1549+ ) ;
15501550 }
15511551
15521552 {
@@ -1562,25 +1562,24 @@ fn test_try_reserve() {
15621562 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
15631563 }
15641564 if guards_against_isize {
1565- if let Err ( CapacityOverflow ) =
1566- ten_u32s. try_reserve ( MAX_CAP / 4 - 9 ) . map_err ( |e| e. kind ( ) )
1567- {
1568- } else {
1569- panic ! ( "isize::MAX + 1 should trigger an overflow!" ) ;
1570- }
1565+ assert_matches ! (
1566+ ten_u32s. try_reserve( MAX_CAP / 4 - 9 ) . map_err( |e| e. kind( ) ) ,
1567+ Err ( CapacityOverflow ) ,
1568+ "isize::MAX + 1 should trigger an overflow!"
1569+ ) ;
15711570 } else {
1572- if let Err ( AllocError { .. } ) =
1573- ten_u32s. try_reserve ( MAX_CAP / 4 - 9 ) . map_err ( |e| e. kind ( ) )
1574- {
1575- } else {
1576- panic ! ( "isize::MAX + 1 should trigger an OOM!" )
1577- }
1571+ assert_matches ! (
1572+ ten_u32s. try_reserve( MAX_CAP / 4 - 9 ) . map_err( |e| e. kind( ) ) ,
1573+ Err ( AllocError { .. } ) ,
1574+ "isize::MAX + 1 should trigger an OOM!"
1575+ ) ;
15781576 }
15791577 // Should fail in the mul-by-size
1580- if let Err ( CapacityOverflow ) = ten_u32s. try_reserve ( MAX_USIZE - 20 ) . map_err ( |e| e. kind ( ) ) {
1581- } else {
1582- panic ! ( "usize::MAX should trigger an overflow!" ) ;
1583- }
1578+ assert_matches ! (
1579+ ten_u32s. try_reserve( MAX_USIZE - 20 ) . map_err( |e| e. kind( ) ) ,
1580+ Err ( CapacityOverflow ) ,
1581+ "usize::MAX should trigger an overflow!"
1582+ ) ;
15841583 }
15851584}
15861585
@@ -1609,33 +1608,29 @@ fn test_try_reserve_exact() {
16091608 }
16101609
16111610 if guards_against_isize {
1612- if let Err ( CapacityOverflow ) =
1613- empty_bytes. try_reserve_exact ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
1614- {
1615- } else {
1616- panic ! ( "isize::MAX + 1 should trigger an overflow!" )
1617- }
1618-
1619- if let Err ( CapacityOverflow ) =
1620- empty_bytes. try_reserve_exact ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
1621- {
1622- } else {
1623- panic ! ( "usize::MAX should trigger an overflow!" )
1624- }
1611+ assert_matches ! (
1612+ empty_bytes. try_reserve_exact( MAX_CAP + 1 ) . map_err( |e| e. kind( ) ) ,
1613+ Err ( CapacityOverflow ) ,
1614+ "isize::MAX + 1 should trigger an overflow!"
1615+ ) ;
1616+
1617+ assert_matches ! (
1618+ empty_bytes. try_reserve_exact( MAX_USIZE ) . map_err( |e| e. kind( ) ) ,
1619+ Err ( CapacityOverflow ) ,
1620+ "usize::MAX should trigger an overflow!"
1621+ ) ;
16251622 } else {
1626- if let Err ( AllocError { .. } ) =
1627- empty_bytes. try_reserve_exact ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
1628- {
1629- } else {
1630- panic ! ( "isize::MAX + 1 should trigger an OOM!" )
1631- }
1623+ assert_matches ! (
1624+ empty_bytes. try_reserve_exact( MAX_CAP + 1 ) . map_err( |e| e. kind( ) ) ,
1625+ Err ( AllocError { .. } ) ,
1626+ "isize::MAX + 1 should trigger an OOM!"
1627+ ) ;
16321628
1633- if let Err ( AllocError { .. } ) =
1634- empty_bytes. try_reserve_exact ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
1635- {
1636- } else {
1637- panic ! ( "usize::MAX should trigger an OOM!" )
1638- }
1629+ assert_matches ! (
1630+ empty_bytes. try_reserve_exact( MAX_USIZE ) . map_err( |e| e. kind( ) ) ,
1631+ Err ( AllocError { .. } ) ,
1632+ "usize::MAX should trigger an OOM!"
1633+ ) ;
16391634 }
16401635 }
16411636
@@ -1653,25 +1648,23 @@ fn test_try_reserve_exact() {
16531648 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
16541649 }
16551650 if guards_against_isize {
1656- if let Err ( CapacityOverflow ) =
1657- ten_bytes. try_reserve_exact ( MAX_CAP - 9 ) . map_err ( |e| e. kind ( ) )
1658- {
1659- } else {
1660- panic ! ( "isize::MAX + 1 should trigger an overflow!" ) ;
1661- }
1662- } else {
1663- if let Err ( AllocError { .. } ) =
1664- ten_bytes. try_reserve_exact ( MAX_CAP - 9 ) . map_err ( |e| e. kind ( ) )
1665- {
1666- } else {
1667- panic ! ( "isize::MAX + 1 should trigger an OOM!" )
1668- }
1669- }
1670- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve_exact ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
1671- {
1651+ assert_matches ! (
1652+ ten_bytes. try_reserve_exact( MAX_CAP - 9 ) . map_err( |e| e. kind( ) ) ,
1653+ Err ( CapacityOverflow ) ,
1654+ "isize::MAX + 1 should trigger an overflow!"
1655+ ) ;
16721656 } else {
1673- panic ! ( "usize::MAX should trigger an overflow!" )
1657+ assert_matches ! (
1658+ ten_bytes. try_reserve_exact( MAX_CAP - 9 ) . map_err( |e| e. kind( ) ) ,
1659+ Err ( AllocError { .. } ) ,
1660+ "isize::MAX + 1 should trigger an OOM!"
1661+ ) ;
16741662 }
1663+ assert_matches ! (
1664+ ten_bytes. try_reserve_exact( MAX_USIZE ) . map_err( |e| e. kind( ) ) ,
1665+ Err ( CapacityOverflow ) ,
1666+ "usize::MAX should trigger an overflow!"
1667+ ) ;
16751668 }
16761669
16771670 {
@@ -1688,26 +1681,23 @@ fn test_try_reserve_exact() {
16881681 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
16891682 }
16901683 if guards_against_isize {
1691- if let Err ( CapacityOverflow ) =
1692- ten_u32s. try_reserve_exact ( MAX_CAP / 4 - 9 ) . map_err ( |e| e. kind ( ) )
1693- {
1694- } else {
1695- panic ! ( "isize::MAX + 1 should trigger an overflow!" ) ;
1696- }
1684+ assert_matches ! (
1685+ ten_u32s. try_reserve_exact( MAX_CAP / 4 - 9 ) . map_err( |e| e. kind( ) ) ,
1686+ Err ( CapacityOverflow ) ,
1687+ "isize::MAX + 1 should trigger an overflow!"
1688+ ) ;
16971689 } else {
1698- if let Err ( AllocError { .. } ) =
1699- ten_u32s. try_reserve_exact ( MAX_CAP / 4 - 9 ) . map_err ( |e| e. kind ( ) )
1700- {
1701- } else {
1702- panic ! ( "isize::MAX + 1 should trigger an OOM!" )
1703- }
1704- }
1705- if let Err ( CapacityOverflow ) =
1706- ten_u32s. try_reserve_exact ( MAX_USIZE - 20 ) . map_err ( |e| e. kind ( ) )
1707- {
1708- } else {
1709- panic ! ( "usize::MAX should trigger an overflow!" )
1710- }
1690+ assert_matches ! (
1691+ ten_u32s. try_reserve_exact( MAX_CAP / 4 - 9 ) . map_err( |e| e. kind( ) ) ,
1692+ Err ( AllocError { .. } ) ,
1693+ "isize::MAX + 1 should trigger an OOM!"
1694+ ) ;
1695+ }
1696+ assert_matches ! (
1697+ ten_u32s. try_reserve_exact( MAX_USIZE - 20 ) . map_err( |e| e. kind( ) ) ,
1698+ Err ( CapacityOverflow ) ,
1699+ "usize::MAX should trigger an overflow!"
1700+ ) ;
17111701 }
17121702}
17131703
0 commit comments