11use std:: borrow:: Cow ;
22use std:: cell:: Cell ;
3- use std:: collections:: TryReserveError :: * ;
3+ use std:: collections:: TryReserveErrorKind :: * ;
44use std:: ops:: Bound ;
55use std:: ops:: Bound :: * ;
66use std:: ops:: RangeBounds ;
@@ -703,35 +703,42 @@ fn test_try_reserve() {
703703 let mut empty_string: String = String :: new ( ) ;
704704
705705 // 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 ( ) ) {
707707 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
708708 }
709709 // 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 ( ) ) {
711711 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
712712 }
713713
714714 if guards_against_isize {
715715 // 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+ {
717719 } else {
718720 panic ! ( "isize::MAX + 1 should trigger an overflow!" )
719721 }
720722
721723 // 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+ {
723726 } else {
724727 panic ! ( "usize::MAX should trigger an overflow!" )
725728 }
726729 } else {
727730 // 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+ {
729734 } else {
730735 panic ! ( "isize::MAX + 1 should trigger an OOM!" )
731736 }
732737
733738 // 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+ {
735742 } else {
736743 panic ! ( "usize::MAX should trigger an OOM!" )
737744 }
@@ -742,25 +749,27 @@ fn test_try_reserve() {
742749 // Same basic idea, but with non-zero len
743750 let mut ten_bytes: String = String :: from ( "0123456789" ) ;
744751
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 ( ) ) {
746753 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
747754 }
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 ( ) ) {
749756 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
750757 }
751758 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+ {
753761 } else {
754762 panic ! ( "isize::MAX + 1 should trigger an overflow!" ) ;
755763 }
756764 } 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+ {
758767 } else {
759768 panic ! ( "isize::MAX + 1 should trigger an OOM!" )
760769 }
761770 }
762771 // 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 ( ) ) {
764773 } else {
765774 panic ! ( "usize::MAX should trigger an overflow!" )
766775 }
@@ -782,30 +791,40 @@ fn test_try_reserve_exact() {
782791 {
783792 let mut empty_string: String = String :: new ( ) ;
784793
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+ {
786796 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
787797 }
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+ {
789800 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
790801 }
791802
792803 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+ {
794807 } else {
795808 panic ! ( "isize::MAX + 1 should trigger an overflow!" )
796809 }
797810
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+ {
799814 } else {
800815 panic ! ( "usize::MAX should trigger an overflow!" )
801816 }
802817 } 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+ {
804821 } else {
805822 panic ! ( "isize::MAX + 1 should trigger an OOM!" )
806823 }
807824
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+ {
809828 } else {
810829 panic ! ( "usize::MAX should trigger an OOM!" )
811830 }
@@ -815,24 +834,33 @@ fn test_try_reserve_exact() {
815834 {
816835 let mut ten_bytes: String = String :: from ( "0123456789" ) ;
817836
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+ {
819840 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
820841 }
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+ {
822845 panic ! ( "isize::MAX shouldn't trigger an overflow!" ) ;
823846 }
824847 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+ {
826851 } else {
827852 panic ! ( "isize::MAX + 1 should trigger an overflow!" ) ;
828853 }
829854 } 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+ {
831858 } else {
832859 panic ! ( "isize::MAX + 1 should trigger an OOM!" )
833860 }
834861 }
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+ {
836864 } else {
837865 panic ! ( "usize::MAX should trigger an overflow!" )
838866 }
0 commit comments