Skip to content

Commit 25d3e14

Browse files
committed
Auto merge of #87843 - kornelski:try_reserve, r=m-ou-se
TryReserveErrorKind tests and inline A small follow-up to #87408
2 parents ccffcaf + 7dca8eb commit 25d3e14

File tree

6 files changed

+282
-301
lines changed

6 files changed

+282
-301
lines changed

library/alloc/src/collections/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub enum TryReserveErrorKind {
111111
issue = "48043"
112112
)]
113113
impl From<TryReserveErrorKind> for TryReserveError {
114+
#[inline]
114115
fn from(kind: TryReserveErrorKind) -> Self {
115116
Self { kind }
116117
}

library/alloc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(allocator_api)]
2+
#![feature(assert_matches)]
23
#![feature(box_syntax)]
34
#![feature(cow_is_borrowed)]
45
#![feature(const_cow_is_borrowed)]

library/alloc/tests/string.rs

+73-80
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::assert_matches::assert_matches;
12
use std::borrow::Cow;
23
use std::cell::Cell;
34
use std::collections::TryReserveErrorKind::*;
@@ -713,35 +714,32 @@ fn test_try_reserve() {
713714

714715
if guards_against_isize {
715716
// 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+
);
722722

723723
// 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+
);
729729
} else {
730730
// 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+
);
737736

738737
// 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+
);
745743
}
746744
}
747745

@@ -756,23 +754,24 @@ fn test_try_reserve() {
756754
panic!("isize::MAX shouldn't trigger an overflow!");
757755
}
758756
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+
);
764762
} 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+
);
770768
}
771769
// 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+
);
776775
}
777776
}
778777

@@ -801,33 +800,29 @@ fn test_try_reserve_exact() {
801800
}
802801

803802
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+
);
817814
} 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+
);
831826
}
832827
}
833828

@@ -845,25 +840,23 @@ fn test_try_reserve_exact() {
845840
panic!("isize::MAX shouldn't trigger an overflow!");
846841
}
847842
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+
);
864848
} 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+
);
866854
}
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+
);
867860
}
868861
}
869862

0 commit comments

Comments
 (0)