1
+ use std:: assert_matches:: assert_matches;
1
2
use std:: borrow:: Cow ;
2
3
use std:: cell:: Cell ;
3
4
use std:: collections:: TryReserveErrorKind :: * ;
@@ -713,35 +714,32 @@ fn test_try_reserve() {
713
714
714
715
if guards_against_isize {
715
716
// Check isize::MAX + 1 does count as overflow
716
- if let Err ( CapacityOverflow ) =
717
- empty_string. try_reserve ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
718
- {
719
- } else {
720
- panic ! ( "isize::MAX + 1 should trigger an overflow!" )
721
- }
717
+ assert_matches ! (
718
+ empty_string. try_reserve( MAX_CAP + 1 ) . map_err( |e| e. kind( ) ) ,
719
+ Err ( CapacityOverflow ) ,
720
+ "isize::MAX + 1 should trigger an overflow!"
721
+ ) ;
722
722
723
723
// Check usize::MAX does count as overflow
724
- if let Err ( CapacityOverflow ) = empty_string . try_reserve ( MAX_USIZE ) . map_err ( |e| e . kind ( ) )
725
- {
726
- } else {
727
- panic ! ( "usize::MAX should trigger an overflow!" )
728
- }
724
+ assert_matches ! (
725
+ empty_string . try_reserve ( MAX_USIZE ) . map_err ( |e| e . kind ( ) ) ,
726
+ Err ( CapacityOverflow ) ,
727
+ "usize::MAX should trigger an overflow!"
728
+ ) ;
729
729
} else {
730
730
// Check isize::MAX + 1 is an OOM
731
- if let Err ( AllocError { .. } ) =
732
- empty_string. try_reserve ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
733
- {
734
- } else {
735
- panic ! ( "isize::MAX + 1 should trigger an OOM!" )
736
- }
731
+ assert_matches ! (
732
+ empty_string. try_reserve( MAX_CAP + 1 ) . map_err( |e| e. kind( ) ) ,
733
+ Err ( AllocError { .. } ) ,
734
+ "isize::MAX + 1 should trigger an OOM!"
735
+ ) ;
737
736
738
737
// Check usize::MAX is an OOM
739
- if let Err ( AllocError { .. } ) =
740
- empty_string. try_reserve ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
741
- {
742
- } else {
743
- panic ! ( "usize::MAX should trigger an OOM!" )
744
- }
738
+ assert_matches ! (
739
+ empty_string. try_reserve( MAX_USIZE ) . map_err( |e| e. kind( ) ) ,
740
+ Err ( AllocError { .. } ) ,
741
+ "usize::MAX should trigger an OOM!"
742
+ ) ;
745
743
}
746
744
}
747
745
@@ -756,23 +754,24 @@ fn test_try_reserve() {
756
754
panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
757
755
}
758
756
if guards_against_isize {
759
- if let Err ( CapacityOverflow ) = ten_bytes . try_reserve ( MAX_CAP - 9 ) . map_err ( |e| e . kind ( ) )
760
- {
761
- } else {
762
- panic ! ( "isize::MAX + 1 should trigger an overflow!" ) ;
763
- }
757
+ assert_matches ! (
758
+ ten_bytes . try_reserve ( MAX_CAP - 9 ) . map_err ( |e| e . kind ( ) ) ,
759
+ Err ( CapacityOverflow ) ,
760
+ "isize::MAX + 1 should trigger an overflow!"
761
+ ) ;
764
762
} else {
765
- if let Err ( AllocError { .. } ) = ten_bytes . try_reserve ( MAX_CAP - 9 ) . map_err ( |e| e . kind ( ) )
766
- {
767
- } else {
768
- panic ! ( "isize::MAX + 1 should trigger an OOM!" )
769
- }
763
+ assert_matches ! (
764
+ ten_bytes . try_reserve ( MAX_CAP - 9 ) . map_err ( |e| e . kind ( ) ) ,
765
+ Err ( AllocError { .. } ) ,
766
+ "isize::MAX + 1 should trigger an OOM!"
767
+ ) ;
770
768
}
771
769
// Should always overflow in the add-to-len
772
- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve ( MAX_USIZE ) . map_err ( |e| e. kind ( ) ) {
773
- } else {
774
- panic ! ( "usize::MAX should trigger an overflow!" )
775
- }
770
+ assert_matches ! (
771
+ ten_bytes. try_reserve( MAX_USIZE ) . map_err( |e| e. kind( ) ) ,
772
+ Err ( CapacityOverflow ) ,
773
+ "usize::MAX should trigger an overflow!"
774
+ ) ;
776
775
}
777
776
}
778
777
@@ -801,33 +800,29 @@ fn test_try_reserve_exact() {
801
800
}
802
801
803
802
if guards_against_isize {
804
- if let Err ( CapacityOverflow ) =
805
- empty_string. try_reserve_exact ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
806
- {
807
- } else {
808
- panic ! ( "isize::MAX + 1 should trigger an overflow!" )
809
- }
810
-
811
- if let Err ( CapacityOverflow ) =
812
- empty_string. try_reserve_exact ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
813
- {
814
- } else {
815
- panic ! ( "usize::MAX should trigger an overflow!" )
816
- }
803
+ assert_matches ! (
804
+ empty_string. try_reserve_exact( MAX_CAP + 1 ) . map_err( |e| e. kind( ) ) ,
805
+ Err ( CapacityOverflow ) ,
806
+ "isize::MAX + 1 should trigger an overflow!"
807
+ ) ;
808
+
809
+ assert_matches ! (
810
+ empty_string. try_reserve_exact( MAX_USIZE ) . map_err( |e| e. kind( ) ) ,
811
+ Err ( CapacityOverflow ) ,
812
+ "usize::MAX should trigger an overflow!"
813
+ ) ;
817
814
} else {
818
- if let Err ( AllocError { .. } ) =
819
- empty_string. try_reserve_exact ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
820
- {
821
- } else {
822
- panic ! ( "isize::MAX + 1 should trigger an OOM!" )
823
- }
824
-
825
- if let Err ( AllocError { .. } ) =
826
- empty_string. try_reserve_exact ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
827
- {
828
- } else {
829
- panic ! ( "usize::MAX should trigger an OOM!" )
830
- }
815
+ assert_matches ! (
816
+ empty_string. try_reserve_exact( MAX_CAP + 1 ) . map_err( |e| e. kind( ) ) ,
817
+ Err ( AllocError { .. } ) ,
818
+ "isize::MAX + 1 should trigger an OOM!"
819
+ ) ;
820
+
821
+ assert_matches ! (
822
+ empty_string. try_reserve_exact( MAX_USIZE ) . map_err( |e| e. kind( ) ) ,
823
+ Err ( AllocError { .. } ) ,
824
+ "usize::MAX should trigger an OOM!"
825
+ ) ;
831
826
}
832
827
}
833
828
@@ -845,25 +840,23 @@ fn test_try_reserve_exact() {
845
840
panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
846
841
}
847
842
if guards_against_isize {
848
- if let Err ( CapacityOverflow ) =
849
- ten_bytes. try_reserve_exact ( MAX_CAP - 9 ) . map_err ( |e| e. kind ( ) )
850
- {
851
- } else {
852
- panic ! ( "isize::MAX + 1 should trigger an overflow!" ) ;
853
- }
854
- } else {
855
- if let Err ( AllocError { .. } ) =
856
- ten_bytes. try_reserve_exact ( MAX_CAP - 9 ) . map_err ( |e| e. kind ( ) )
857
- {
858
- } else {
859
- panic ! ( "isize::MAX + 1 should trigger an OOM!" )
860
- }
861
- }
862
- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve_exact ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
863
- {
843
+ assert_matches ! (
844
+ ten_bytes. try_reserve_exact( MAX_CAP - 9 ) . map_err( |e| e. kind( ) ) ,
845
+ Err ( CapacityOverflow ) ,
846
+ "isize::MAX + 1 should trigger an overflow!"
847
+ ) ;
864
848
} else {
865
- panic ! ( "usize::MAX should trigger an overflow!" )
849
+ assert_matches ! (
850
+ ten_bytes. try_reserve_exact( MAX_CAP - 9 ) . map_err( |e| e. kind( ) ) ,
851
+ Err ( AllocError { .. } ) ,
852
+ "isize::MAX + 1 should trigger an OOM!"
853
+ ) ;
866
854
}
855
+ assert_matches ! (
856
+ ten_bytes. try_reserve_exact( MAX_USIZE ) . map_err( |e| e. kind( ) ) ,
857
+ Err ( CapacityOverflow ) ,
858
+ "usize::MAX should trigger an overflow!"
859
+ ) ;
867
860
}
868
861
}
869
862
0 commit comments