diff options
| author | bors <bors@rust-lang.org> | 2021-08-07 01:26:15 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2021-08-07 01:26:15 +0000 |
| commit | 996ff2e0a0f911f52bb1de6bdd0cfd5704de1fc9 (patch) | |
| tree | f10bdc2b1742f123ce140d43cb11d8f07dabfe92 /library/alloc/tests | |
| parent | db3cb435c1197ef3e3919c03b7f81ca8bffbd007 (diff) | |
| parent | a294aa8d3d7b73b7de60c629ae202194cffeb2f4 (diff) | |
| download | rust-996ff2e0a0f911f52bb1de6bdd0cfd5704de1fc9.tar.gz rust-996ff2e0a0f911f52bb1de6bdd0cfd5704de1fc9.zip | |
Auto merge of #87408 - kornelski:try_reserve_error, r=yaahc
Hide allocator details from TryReserveError I think there's [no need for TryReserveError to carry detailed information](https://github.com/rust-lang/rust/issues/48043#issuecomment-825139280), but I wouldn't want that issue to delay stabilization of the `try_reserve` feature. So I'm proposing to stabilize `try_reserve` with a `TryReserveError` as an opaque structure, and if needed, expose error details later. This PR moves the `enum` to an unstable inner `TryReserveErrorKind` that lives under a separate feature flag. `TryReserveErrorKind` could possibly be left as an implementation detail forever, and the `TryReserveError` get methods such as `allocation_size() -> Option<usize>` or `layout() -> Option<Layout>` instead, or the details could be dropped completely to make try-reserve errors just a unit struct, and thus smaller and cheaper.
Diffstat (limited to 'library/alloc/tests')
| -rw-r--r-- | library/alloc/tests/lib.rs | 1 | ||||
| -rw-r--r-- | library/alloc/tests/string.rs | 74 | ||||
| -rw-r--r-- | library/alloc/tests/vec.rs | 109 | ||||
| -rw-r--r-- | library/alloc/tests/vec_deque.rs | 100 |
4 files changed, 197 insertions, 87 deletions
diff --git a/library/alloc/tests/lib.rs b/library/alloc/tests/lib.rs index 3143afa269d..b3e048a6587 100644 --- a/library/alloc/tests/lib.rs +++ b/library/alloc/tests/lib.rs @@ -8,6 +8,7 @@ #![feature(pattern)] #![feature(trusted_len)] #![feature(try_reserve)] +#![feature(try_reserve_kind)] #![feature(unboxed_closures)] #![feature(associated_type_bounds)] #![feature(binary_heap_into_iter_sorted)] diff --git a/library/alloc/tests/string.rs b/library/alloc/tests/string.rs index 9ec0ee97ab9..cf9d734a9d5 100644 --- a/library/alloc/tests/string.rs +++ b/library/alloc/tests/string.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; use std::cell::Cell; -use std::collections::TryReserveError::*; +use std::collections::TryReserveErrorKind::*; use std::ops::Bound; use std::ops::Bound::*; use std::ops::RangeBounds; @@ -703,35 +703,42 @@ fn test_try_reserve() { let mut empty_string: String = String::new(); // Check isize::MAX doesn't count as an overflow - if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_CAP) { + if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_CAP).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } // Play it again, frank! (just to be sure) - if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_CAP) { + if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_CAP).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { // Check isize::MAX + 1 does count as overflow - if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_CAP + 1) { + if let Err(CapacityOverflow) = + empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!") } // Check usize::MAX does count as overflow - if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_USIZE) { + if let Err(CapacityOverflow) = empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } } else { // Check isize::MAX + 1 is an OOM - if let Err(AllocError { .. }) = empty_string.try_reserve(MAX_CAP + 1) { + if let Err(AllocError { .. }) = + empty_string.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } // Check usize::MAX is an OOM - if let Err(AllocError { .. }) = empty_string.try_reserve(MAX_USIZE) { + if let Err(AllocError { .. }) = + empty_string.try_reserve(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an OOM!") } @@ -742,25 +749,27 @@ fn test_try_reserve() { // Same basic idea, but with non-zero len let mut ten_bytes: String = String::from("0123456789"); - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9) { + if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } // Should always overflow in the add-to-len - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) { } else { panic!("usize::MAX should trigger an overflow!") } @@ -782,30 +791,40 @@ fn test_try_reserve_exact() { { let mut empty_string: String = String::new(); - if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_CAP) { + if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_CAP).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_CAP) { + if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_CAP).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_CAP + 1) { + if let Err(CapacityOverflow) = + empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!") } - if let Err(CapacityOverflow) = empty_string.try_reserve_exact(MAX_USIZE) { + if let Err(CapacityOverflow) = + empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } } else { - if let Err(AllocError { .. }) = empty_string.try_reserve_exact(MAX_CAP + 1) { + if let Err(AllocError { .. }) = + empty_string.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } - if let Err(AllocError { .. }) = empty_string.try_reserve_exact(MAX_USIZE) { + if let Err(AllocError { .. }) = + empty_string.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an OOM!") } @@ -815,24 +834,33 @@ fn test_try_reserve_exact() { { let mut ten_bytes: String = String::from("0123456789"); - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 10) { + if let Err(CapacityOverflow) = + ten_bytes.try_reserve_exact(MAX_CAP - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 10) { + if let Err(CapacityOverflow) = + ten_bytes.try_reserve_exact(MAX_CAP - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { + if let Err(CapacityOverflow) = + ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocError { .. }) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { + if let Err(AllocError { .. }) = + ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } diff --git a/library/alloc/tests/vec.rs b/library/alloc/tests/vec.rs index 3b723701680..d3a6dcfddc5 100644 --- a/library/alloc/tests/vec.rs +++ b/library/alloc/tests/vec.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; use std::cell::Cell; -use std::collections::TryReserveError::*; +use std::collections::TryReserveErrorKind::*; use std::fmt::Debug; use std::iter::InPlaceIterable; use std::mem::{size_of, swap}; @@ -1478,35 +1478,41 @@ fn test_try_reserve() { let mut empty_bytes: Vec<u8> = Vec::new(); // Check isize::MAX doesn't count as an overflow - if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP) { + if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } // Play it again, frank! (just to be sure) - if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP) { + if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { // Check isize::MAX + 1 does count as overflow - if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP + 1) { + if let Err(CapacityOverflow) = + empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!") } // Check usize::MAX does count as overflow - if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_USIZE) { + if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } } else { // Check isize::MAX + 1 is an OOM - if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_CAP + 1) { + if let Err(AllocError { .. }) = + empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } // Check usize::MAX is an OOM - if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE) { + if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an OOM!") } @@ -1517,25 +1523,27 @@ fn test_try_reserve() { // Same basic idea, but with non-zero len let mut ten_bytes: Vec<u8> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9) { + if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } // Should always overflow in the add-to-len - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) { } else { panic!("usize::MAX should trigger an overflow!") } @@ -1545,25 +1553,31 @@ fn test_try_reserve() { // Same basic idea, but with interesting type size let mut ten_u32s: Vec<u32> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 10) { + if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 10) { + if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 9) { + if let Err(CapacityOverflow) = + ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocError { .. }) = ten_u32s.try_reserve(MAX_CAP / 4 - 9) { + if let Err(AllocError { .. }) = + ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } // Should fail in the mul-by-size - if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_USIZE - 20) { + if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()) { } else { panic!("usize::MAX should trigger an overflow!"); } @@ -1585,30 +1599,40 @@ fn test_try_reserve_exact() { { let mut empty_bytes: Vec<u8> = Vec::new(); - if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP) { + if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP) { + if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP + 1) { + if let Err(CapacityOverflow) = + empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!") } - if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_USIZE) { + if let Err(CapacityOverflow) = + empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } } else { - if let Err(AllocError { .. }) = empty_bytes.try_reserve_exact(MAX_CAP + 1) { + if let Err(AllocError { .. }) = + empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } - if let Err(AllocError { .. }) = empty_bytes.try_reserve_exact(MAX_USIZE) { + if let Err(AllocError { .. }) = + empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an OOM!") } @@ -1618,24 +1642,33 @@ fn test_try_reserve_exact() { { let mut ten_bytes: Vec<u8> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 10) { + if let Err(CapacityOverflow) = + ten_bytes.try_reserve_exact(MAX_CAP - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 10) { + if let Err(CapacityOverflow) = + ten_bytes.try_reserve_exact(MAX_CAP - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { + if let Err(CapacityOverflow) = + ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocError { .. }) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { + if let Err(AllocError { .. }) = + ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } @@ -1644,24 +1677,34 @@ fn test_try_reserve_exact() { { let mut ten_u32s: Vec<u32> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_CAP / 4 - 10) { + if let Err(CapacityOverflow) = + ten_u32s.try_reserve_exact(MAX_CAP / 4 - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_CAP / 4 - 10) { + if let Err(CapacityOverflow) = + ten_u32s.try_reserve_exact(MAX_CAP / 4 - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9) { + if let Err(CapacityOverflow) = + ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocError { .. }) = ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9) { + if let Err(AllocError { .. }) = + ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } - if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_USIZE - 20) { + if let Err(CapacityOverflow) = + ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } diff --git a/library/alloc/tests/vec_deque.rs b/library/alloc/tests/vec_deque.rs index d7140cf9759..e9fd19d76ea 100644 --- a/library/alloc/tests/vec_deque.rs +++ b/library/alloc/tests/vec_deque.rs @@ -1,4 +1,4 @@ -use std::collections::TryReserveError::*; +use std::collections::TryReserveErrorKind::*; use std::collections::{vec_deque::Drain, VecDeque}; use std::fmt::Debug; use std::mem::size_of; @@ -1171,23 +1171,26 @@ fn test_try_reserve() { let mut empty_bytes: VecDeque<u8> = VecDeque::new(); // Check isize::MAX doesn't count as an overflow - if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP) { + if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } // Play it again, frank! (just to be sure) - if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP) { + if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { // Check isize::MAX + 1 does count as overflow - if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP + 1) { + if let Err(CapacityOverflow) = + empty_bytes.try_reserve(MAX_CAP + 1).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!") } // Check usize::MAX does count as overflow - if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_USIZE) { + if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } @@ -1196,7 +1199,7 @@ fn test_try_reserve() { // VecDeque starts with capacity 7, always adds 1 to the capacity // and also rounds the number to next power of 2 so this is the // furthest we can go without triggering CapacityOverflow - if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_CAP) { + if let Err(AllocError { .. }) = empty_bytes.try_reserve(MAX_CAP).map_err(|e| e.kind()) { } else { panic!("isize::MAX + 1 should trigger an OOM!") } @@ -1207,25 +1210,27 @@ fn test_try_reserve() { // Same basic idea, but with non-zero len let mut ten_bytes: VecDeque<u8> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10].into_iter().collect(); - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 10).map_err(|e| e.kind()) { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9) { + if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } // Should always overflow in the add-to-len - if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_USIZE).map_err(|e| e.kind()) { } else { panic!("usize::MAX should trigger an overflow!") } @@ -1235,25 +1240,31 @@ fn test_try_reserve() { // Same basic idea, but with interesting type size let mut ten_u32s: VecDeque<u32> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10].into_iter().collect(); - if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 10) { + if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 10) { + if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 9) { + if let Err(CapacityOverflow) = + ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocError { .. }) = ten_u32s.try_reserve(MAX_CAP / 4 - 9) { + if let Err(AllocError { .. }) = + ten_u32s.try_reserve(MAX_CAP / 4 - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } // Should fail in the mul-by-size - if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_USIZE - 20) { + if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_USIZE - 20).map_err(|e| e.kind()) { } else { panic!("usize::MAX should trigger an overflow!"); } @@ -1275,20 +1286,26 @@ fn test_try_reserve_exact() { { let mut empty_bytes: VecDeque<u8> = VecDeque::new(); - if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP) { + if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP) { + if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP + 1) { + if let Err(CapacityOverflow) = + empty_bytes.try_reserve_exact(MAX_CAP + 1).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!") } - if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_USIZE) { + if let Err(CapacityOverflow) = + empty_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } @@ -1297,7 +1314,9 @@ fn test_try_reserve_exact() { // VecDeque starts with capacity 7, always adds 1 to the capacity // and also rounds the number to next power of 2 so this is the // furthest we can go without triggering CapacityOverflow - if let Err(AllocError { .. }) = empty_bytes.try_reserve_exact(MAX_CAP) { + if let Err(AllocError { .. }) = + empty_bytes.try_reserve_exact(MAX_CAP).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } @@ -1307,24 +1326,33 @@ fn test_try_reserve_exact() { { let mut ten_bytes: VecDeque<u8> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10].into_iter().collect(); - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 10) { + if let Err(CapacityOverflow) = + ten_bytes.try_reserve_exact(MAX_CAP - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 10) { + if let Err(CapacityOverflow) = + ten_bytes.try_reserve_exact(MAX_CAP - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { + if let Err(CapacityOverflow) = + ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocError { .. }) = ten_bytes.try_reserve_exact(MAX_CAP - 9) { + if let Err(AllocError { .. }) = + ten_bytes.try_reserve_exact(MAX_CAP - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } - if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE) { + if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } @@ -1333,24 +1361,34 @@ fn test_try_reserve_exact() { { let mut ten_u32s: VecDeque<u32> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10].into_iter().collect(); - if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_CAP / 4 - 10) { + if let Err(CapacityOverflow) = + ten_u32s.try_reserve_exact(MAX_CAP / 4 - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } - if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_CAP / 4 - 10) { + if let Err(CapacityOverflow) = + ten_u32s.try_reserve_exact(MAX_CAP / 4 - 10).map_err(|e| e.kind()) + { panic!("isize::MAX shouldn't trigger an overflow!"); } if guards_against_isize { - if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9) { + if let Err(CapacityOverflow) = + ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an overflow!"); } } else { - if let Err(AllocError { .. }) = ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9) { + if let Err(AllocError { .. }) = + ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9).map_err(|e| e.kind()) + { } else { panic!("isize::MAX + 1 should trigger an OOM!") } } - if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_USIZE - 20) { + if let Err(CapacityOverflow) = + ten_u32s.try_reserve_exact(MAX_USIZE - 20).map_err(|e| e.kind()) + { } else { panic!("usize::MAX should trigger an overflow!") } |
