1
1
use std:: borrow:: Cow ;
2
2
use std:: cell:: Cell ;
3
- use std:: collections:: TryReserveError :: * ;
3
+ use std:: collections:: TryReserveErrorKind :: * ;
4
4
use std:: ops:: Bound ;
5
5
use std:: ops:: Bound :: * ;
6
6
use std:: ops:: RangeBounds ;
@@ -703,35 +703,42 @@ fn test_try_reserve() {
703
703
let mut empty_string: String = String :: new ( ) ;
704
704
705
705
// Check isize::MAX doesn't count as an overflow
706
- if let Err ( CapacityOverflow ) = empty_string. try_reserve ( MAX_CAP ) {
706
+ if let Err ( CapacityOverflow ) = empty_string. try_reserve ( MAX_CAP ) . map_err ( |e| e . kind ( ) ) {
707
707
panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
708
708
}
709
709
// Play it again, frank! (just to be sure)
710
- if let Err ( CapacityOverflow ) = empty_string. try_reserve ( MAX_CAP ) {
710
+ if let Err ( CapacityOverflow ) = empty_string. try_reserve ( MAX_CAP ) . map_err ( |e| e . kind ( ) ) {
711
711
panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
712
712
}
713
713
714
714
if guards_against_isize {
715
715
// Check isize::MAX + 1 does count as overflow
716
- if let Err ( CapacityOverflow ) = empty_string. try_reserve ( MAX_CAP + 1 ) {
716
+ if let Err ( CapacityOverflow ) =
717
+ empty_string. try_reserve ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
718
+ {
717
719
} else {
718
720
panic ! ( "isize::MAX + 1 should trigger an overflow!" )
719
721
}
720
722
721
723
// Check usize::MAX does count as overflow
722
- if let Err ( CapacityOverflow ) = empty_string. try_reserve ( MAX_USIZE ) {
724
+ if let Err ( CapacityOverflow ) = empty_string. try_reserve ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
725
+ {
723
726
} else {
724
727
panic ! ( "usize::MAX should trigger an overflow!" )
725
728
}
726
729
} else {
727
730
// Check isize::MAX + 1 is an OOM
728
- if let Err ( AllocError { .. } ) = empty_string. try_reserve ( MAX_CAP + 1 ) {
731
+ if let Err ( AllocError { .. } ) =
732
+ empty_string. try_reserve ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
733
+ {
729
734
} else {
730
735
panic ! ( "isize::MAX + 1 should trigger an OOM!" )
731
736
}
732
737
733
738
// Check usize::MAX is an OOM
734
- if let Err ( AllocError { .. } ) = empty_string. try_reserve ( MAX_USIZE ) {
739
+ if let Err ( AllocError { .. } ) =
740
+ empty_string. try_reserve ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
741
+ {
735
742
} else {
736
743
panic ! ( "usize::MAX should trigger an OOM!" )
737
744
}
@@ -742,25 +749,27 @@ fn test_try_reserve() {
742
749
// Same basic idea, but with non-zero len
743
750
let mut ten_bytes: String = String :: from ( "0123456789" ) ;
744
751
745
- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve ( MAX_CAP - 10 ) {
752
+ if let Err ( CapacityOverflow ) = ten_bytes. try_reserve ( MAX_CAP - 10 ) . map_err ( |e| e . kind ( ) ) {
746
753
panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
747
754
}
748
- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve ( MAX_CAP - 10 ) {
755
+ if let Err ( CapacityOverflow ) = ten_bytes. try_reserve ( MAX_CAP - 10 ) . map_err ( |e| e . kind ( ) ) {
749
756
panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
750
757
}
751
758
if guards_against_isize {
752
- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve ( MAX_CAP - 9 ) {
759
+ if let Err ( CapacityOverflow ) = ten_bytes. try_reserve ( MAX_CAP - 9 ) . map_err ( |e| e. kind ( ) )
760
+ {
753
761
} else {
754
762
panic ! ( "isize::MAX + 1 should trigger an overflow!" ) ;
755
763
}
756
764
} else {
757
- if let Err ( AllocError { .. } ) = ten_bytes. try_reserve ( MAX_CAP - 9 ) {
765
+ if let Err ( AllocError { .. } ) = ten_bytes. try_reserve ( MAX_CAP - 9 ) . map_err ( |e| e. kind ( ) )
766
+ {
758
767
} else {
759
768
panic ! ( "isize::MAX + 1 should trigger an OOM!" )
760
769
}
761
770
}
762
771
// Should always overflow in the add-to-len
763
- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve ( MAX_USIZE ) {
772
+ if let Err ( CapacityOverflow ) = ten_bytes. try_reserve ( MAX_USIZE ) . map_err ( |e| e . kind ( ) ) {
764
773
} else {
765
774
panic ! ( "usize::MAX should trigger an overflow!" )
766
775
}
@@ -782,30 +791,40 @@ fn test_try_reserve_exact() {
782
791
{
783
792
let mut empty_string: String = String :: new ( ) ;
784
793
785
- if let Err ( CapacityOverflow ) = empty_string. try_reserve_exact ( MAX_CAP ) {
794
+ if let Err ( CapacityOverflow ) = empty_string. try_reserve_exact ( MAX_CAP ) . map_err ( |e| e. kind ( ) )
795
+ {
786
796
panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
787
797
}
788
- if let Err ( CapacityOverflow ) = empty_string. try_reserve_exact ( MAX_CAP ) {
798
+ if let Err ( CapacityOverflow ) = empty_string. try_reserve_exact ( MAX_CAP ) . map_err ( |e| e. kind ( ) )
799
+ {
789
800
panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
790
801
}
791
802
792
803
if guards_against_isize {
793
- if let Err ( CapacityOverflow ) = empty_string. try_reserve_exact ( MAX_CAP + 1 ) {
804
+ if let Err ( CapacityOverflow ) =
805
+ empty_string. try_reserve_exact ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
806
+ {
794
807
} else {
795
808
panic ! ( "isize::MAX + 1 should trigger an overflow!" )
796
809
}
797
810
798
- if let Err ( CapacityOverflow ) = empty_string. try_reserve_exact ( MAX_USIZE ) {
811
+ if let Err ( CapacityOverflow ) =
812
+ empty_string. try_reserve_exact ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
813
+ {
799
814
} else {
800
815
panic ! ( "usize::MAX should trigger an overflow!" )
801
816
}
802
817
} else {
803
- if let Err ( AllocError { .. } ) = empty_string. try_reserve_exact ( MAX_CAP + 1 ) {
818
+ if let Err ( AllocError { .. } ) =
819
+ empty_string. try_reserve_exact ( MAX_CAP + 1 ) . map_err ( |e| e. kind ( ) )
820
+ {
804
821
} else {
805
822
panic ! ( "isize::MAX + 1 should trigger an OOM!" )
806
823
}
807
824
808
- if let Err ( AllocError { .. } ) = empty_string. try_reserve_exact ( MAX_USIZE ) {
825
+ if let Err ( AllocError { .. } ) =
826
+ empty_string. try_reserve_exact ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
827
+ {
809
828
} else {
810
829
panic ! ( "usize::MAX should trigger an OOM!" )
811
830
}
@@ -815,24 +834,33 @@ fn test_try_reserve_exact() {
815
834
{
816
835
let mut ten_bytes: String = String :: from ( "0123456789" ) ;
817
836
818
- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve_exact ( MAX_CAP - 10 ) {
837
+ if let Err ( CapacityOverflow ) =
838
+ ten_bytes. try_reserve_exact ( MAX_CAP - 10 ) . map_err ( |e| e. kind ( ) )
839
+ {
819
840
panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
820
841
}
821
- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve_exact ( MAX_CAP - 10 ) {
842
+ if let Err ( CapacityOverflow ) =
843
+ ten_bytes. try_reserve_exact ( MAX_CAP - 10 ) . map_err ( |e| e. kind ( ) )
844
+ {
822
845
panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
823
846
}
824
847
if guards_against_isize {
825
- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve_exact ( MAX_CAP - 9 ) {
848
+ if let Err ( CapacityOverflow ) =
849
+ ten_bytes. try_reserve_exact ( MAX_CAP - 9 ) . map_err ( |e| e. kind ( ) )
850
+ {
826
851
} else {
827
852
panic ! ( "isize::MAX + 1 should trigger an overflow!" ) ;
828
853
}
829
854
} else {
830
- if let Err ( AllocError { .. } ) = ten_bytes. try_reserve_exact ( MAX_CAP - 9 ) {
855
+ if let Err ( AllocError { .. } ) =
856
+ ten_bytes. try_reserve_exact ( MAX_CAP - 9 ) . map_err ( |e| e. kind ( ) )
857
+ {
831
858
} else {
832
859
panic ! ( "isize::MAX + 1 should trigger an OOM!" )
833
860
}
834
861
}
835
- if let Err ( CapacityOverflow ) = ten_bytes. try_reserve_exact ( MAX_USIZE ) {
862
+ if let Err ( CapacityOverflow ) = ten_bytes. try_reserve_exact ( MAX_USIZE ) . map_err ( |e| e. kind ( ) )
863
+ {
836
864
} else {
837
865
panic ! ( "usize::MAX should trigger an overflow!" )
838
866
}
0 commit comments