use std::borrow::Cow;
+use std::collections::TryReserveError::*;
use std::mem::size_of;
-use std::{usize, isize};
use std::vec::{Drain, IntoIter};
-use std::collections::TryReserveError::*;
+use std::{isize, usize};
struct DropCounter<'a> {
count: &'a mut u32,
let (mut count_x, mut count_y) = (0, 0);
{
- let mut tv = TwoVec {
- x: Vec::new(),
- y: Vec::new(),
- };
+ let mut tv = TwoVec { x: Vec::new(), y: Vec::new() };
tv.x.push(DropCounter { count: &mut count_x });
tv.y.push(DropCounter { count: &mut count_y });
assert_eq!(v, [1, 2, 3, 4, 5, 6, 7]);
}
+#[test]
+fn test_remove_item() {
+ let mut v = vec![1, 2, 3];
+ v.remove_item(&1);
+
+ assert_eq!(v.len(), 2);
+ assert_eq!(v, [2, 3]);
+
+ let mut w = vec![1, 2, 3];
+ w.remove_item(&4);
+
+ assert_eq!(w.len(), 3);
+ w.remove_item(&4);
+}
+
#[test]
fn test_slice_from_mut() {
let mut values = vec![1, 2, 3, 4, 5];
assert_eq!(vec, ["foo", "bar", "baz", "bar"]);
let mut vec = vec![("foo", 1), ("foo", 2), ("bar", 3), ("bar", 4), ("bar", 5)];
- vec.dedup_by(|a, b| a.0 == b.0 && { b.1 += a.1; true });
+ vec.dedup_by(|a, b| {
+ a.0 == b.0 && {
+ b.1 += a.1;
+ true
+ }
+ });
assert_eq!(vec, [("foo", 3), ("bar", 12)]);
}
#[test]
fn test_partition() {
- assert_eq!(vec![].into_iter().partition(|x: &i32| *x < 3),
- (vec![], vec![]));
- assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 4),
- (vec![1, 2, 3], vec![]));
- assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 2),
- (vec![1], vec![2, 3]));
- assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 0),
- (vec![], vec![1, 2, 3]));
+ assert_eq!(vec![].into_iter().partition(|x: &i32| *x < 3), (vec![], vec![]));
+ assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 4), (vec![1, 2, 3], vec![]));
+ assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 2), (vec![1], vec![2, 3]));
+ assert_eq!(vec![1, 2, 3].into_iter().partition(|x| *x < 0), (vec![], vec![1, 2, 3]));
}
#[test]
#[test]
fn test_drain_range() {
let mut v = vec![1, 2, 3, 4, 5];
- for _ in v.drain(4..) {
- }
+ for _ in v.drain(4..) {}
assert_eq!(v, &[1, 2, 3, 4]);
let mut v: Vec<_> = (1..6).map(|x| x.to_string()).collect();
- for _ in v.drain(1..4) {
- }
+ for _ in v.drain(1..4) {}
assert_eq!(v, &[1.to_string(), 5.to_string()]);
let mut v: Vec<_> = (1..6).map(|x| x.to_string()).collect();
- for _ in v.drain(1..4).rev() {
- }
+ for _ in v.drain(1..4).rev() {}
assert_eq!(v, &[1.to_string(), 5.to_string()]);
let mut v: Vec<_> = vec![(); 5];
- for _ in v.drain(1..4).rev() {
- }
+ for _ in v.drain(1..4).rev() {}
assert_eq!(v, &[(), ()]);
}
#[test]
fn test_drain_inclusive_range() {
let mut v = vec!['a', 'b', 'c', 'd', 'e'];
- for _ in v.drain(1..=3) {
- }
+ for _ in v.drain(1..=3) {}
assert_eq!(v, &['a', 'e']);
let mut v: Vec<_> = (0..=5).map(|x| x.to_string()).collect();
- for _ in v.drain(1..=5) {
- }
+ for _ in v.drain(1..=5) {}
assert_eq!(v, &["0".to_string()]);
let mut v: Vec<String> = (0..=5).map(|x| x.to_string()).collect();
- for _ in v.drain(0..=5) {
- }
+ for _ in v.drain(0..=5) {}
assert_eq!(v, Vec::<String>::new());
let mut v: Vec<_> = (0..=5).map(|x| x.to_string()).collect();
- for _ in v.drain(0..=3) {
- }
+ for _ in v.drain(0..=3) {}
assert_eq!(v, &["4".to_string(), "5".to_string()]);
let mut v: Vec<_> = (0..=1).map(|x| x.to_string()).collect();
- for _ in v.drain(..=0) {
- }
+ for _ in v.drain(..=0) {}
assert_eq!(v, &["1".to_string()]);
}
#[test]
fn test_drain_max_vec_size() {
let mut v = Vec::<()>::with_capacity(usize::max_value());
- unsafe { v.set_len(usize::max_value()); }
- for _ in v.drain(usize::max_value() - 1..) {
+ unsafe {
+ v.set_len(usize::max_value());
}
+ for _ in v.drain(usize::max_value() - 1..) {}
assert_eq!(v.len(), usize::max_value() - 1);
let mut v = Vec::<()>::with_capacity(usize::max_value());
- unsafe { v.set_len(usize::max_value()); }
- for _ in v.drain(usize::max_value() - 1..=usize::max_value() - 1) {
+ unsafe {
+ v.set_len(usize::max_value());
}
+ for _ in v.drain(usize::max_value() - 1..=usize::max_value() - 1) {}
assert_eq!(v.len(), usize::max_value() - 1);
}
#[test]
fn drain_filter_complex() {
-
- { // [+xxx++++++xxxxx++++x+x++]
- let mut vec = vec![1,
- 2, 4, 6,
- 7, 9, 11, 13, 15, 17,
- 18, 20, 22, 24, 26,
- 27, 29, 31, 33,
- 34,
- 35,
- 36,
- 37, 39];
+ {
+ // [+xxx++++++xxxxx++++x+x++]
+ let mut vec = vec![
+ 1, 2, 4, 6, 7, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 34, 35, 36, 37,
+ 39,
+ ];
let removed = vec.drain_filter(|x| *x % 2 == 0).collect::<Vec<_>>();
assert_eq!(removed.len(), 10);
assert_eq!(vec, vec![1, 7, 9, 11, 13, 15, 17, 27, 29, 31, 33, 35, 37, 39]);
}
- { // [xxx++++++xxxxx++++x+x++]
- let mut vec = vec![2, 4, 6,
- 7, 9, 11, 13, 15, 17,
- 18, 20, 22, 24, 26,
- 27, 29, 31, 33,
- 34,
- 35,
- 36,
- 37, 39];
+ {
+ // [xxx++++++xxxxx++++x+x++]
+ let mut vec = vec![
+ 2, 4, 6, 7, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 34, 35, 36, 37, 39,
+ ];
let removed = vec.drain_filter(|x| *x % 2 == 0).collect::<Vec<_>>();
assert_eq!(removed.len(), 10);
assert_eq!(vec, vec![7, 9, 11, 13, 15, 17, 27, 29, 31, 33, 35, 37, 39]);
}
- { // [xxx++++++xxxxx++++x+x]
- let mut vec = vec![2, 4, 6,
- 7, 9, 11, 13, 15, 17,
- 18, 20, 22, 24, 26,
- 27, 29, 31, 33,
- 34,
- 35,
- 36];
+ {
+ // [xxx++++++xxxxx++++x+x]
+ let mut vec =
+ vec![2, 4, 6, 7, 9, 11, 13, 15, 17, 18, 20, 22, 24, 26, 27, 29, 31, 33, 34, 35, 36];
let removed = vec.drain_filter(|x| *x % 2 == 0).collect::<Vec<_>>();
assert_eq!(removed.len(), 10);
assert_eq!(vec, vec![7, 9, 11, 13, 15, 17, 27, 29, 31, 33, 35]);
}
- { // [xxxxxxxxxx+++++++++++]
- let mut vec = vec![2, 4, 6, 8, 10, 12, 14, 16, 18, 20,
- 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
+ {
+ // [xxxxxxxxxx+++++++++++]
+ let mut vec = vec![2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19];
let removed = vec.drain_filter(|x| *x % 2 == 0).collect::<Vec<_>>();
assert_eq!(removed.len(), 10);
assert_eq!(vec, vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19]);
}
- { // [+++++++++++xxxxxxxxxx]
- let mut vec = vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19,
- 2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
+ {
+ // [+++++++++++xxxxxxxxxx]
+ let mut vec = vec![1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20];
let removed = vec.drain_filter(|x| *x % 2 == 0).collect::<Vec<_>>();
assert_eq!(removed.len(), 10);
#[test]
#[cfg_attr(miri, ignore)] // Miri does not support signalling OOM
fn test_try_reserve() {
-
// These are the interesting cases:
// * exactly isize::MAX should never trigger a CapacityOverflow (can be OOM)
// * > isize::MAX should always fail
if guards_against_isize {
// Check isize::MAX + 1 does count as overflow
if let Err(CapacityOverflow) = empty_bytes.try_reserve(MAX_CAP + 1) {
- } else { panic!("isize::MAX + 1 should trigger an overflow!") }
+ } 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) {
- } else { panic!("usize::MAX should trigger an overflow!") }
+ } 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) {
- } else { panic!("isize::MAX + 1 should trigger an OOM!") }
+ } 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) {
- } else { panic!("usize::MAX should trigger an OOM!") }
+ } else {
+ panic!("usize::MAX should trigger an OOM!")
+ }
}
}
-
{
// 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 guards_against_isize {
if let Err(CapacityOverflow) = ten_bytes.try_reserve(MAX_CAP - 9) {
- } else { panic!("isize::MAX + 1 should trigger an overflow!"); }
+ } else {
+ panic!("isize::MAX + 1 should trigger an overflow!");
+ }
} else {
if let Err(AllocError { .. }) = ten_bytes.try_reserve(MAX_CAP - 9) {
- } else { panic!("isize::MAX + 1 should trigger an OOM!") }
+ } 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) {
- } else { panic!("usize::MAX should trigger an overflow!") }
+ } else {
+ panic!("usize::MAX should trigger an overflow!")
+ }
}
-
{
// 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) {
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) {
panic!("isize::MAX shouldn't trigger an overflow!");
}
if guards_against_isize {
- if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP/4 - 9) {
- } else { panic!("isize::MAX + 1 should trigger an overflow!"); }
+ if let Err(CapacityOverflow) = ten_u32s.try_reserve(MAX_CAP / 4 - 9) {
+ } else {
+ panic!("isize::MAX + 1 should trigger an overflow!");
+ }
} else {
- if let Err(AllocError { .. }) = ten_u32s.try_reserve(MAX_CAP/4 - 9) {
- } else { panic!("isize::MAX + 1 should trigger an OOM!") }
+ if let Err(AllocError { .. }) = ten_u32s.try_reserve(MAX_CAP / 4 - 9) {
+ } 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) {
panic!("usize::MAX should trigger an overflow!");
}
}
-
}
#[test]
#[cfg_attr(miri, ignore)] // Miri does not support signalling OOM
fn test_try_reserve_exact() {
-
// This is exactly the same as test_try_reserve with the method changed.
// See that test for comments.
if guards_against_isize {
if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_CAP + 1) {
- } else { panic!("isize::MAX + 1 should trigger an overflow!") }
+ } else {
+ panic!("isize::MAX + 1 should trigger an overflow!")
+ }
if let Err(CapacityOverflow) = empty_bytes.try_reserve_exact(MAX_USIZE) {
- } else { panic!("usize::MAX should trigger an overflow!") }
+ } else {
+ panic!("usize::MAX should trigger an overflow!")
+ }
} else {
if let Err(AllocError { .. }) = empty_bytes.try_reserve_exact(MAX_CAP + 1) {
- } else { panic!("isize::MAX + 1 should trigger an OOM!") }
+ } else {
+ panic!("isize::MAX + 1 should trigger an OOM!")
+ }
if let Err(AllocError { .. }) = empty_bytes.try_reserve_exact(MAX_USIZE) {
- } else { panic!("usize::MAX should trigger an OOM!") }
+ } else {
+ panic!("usize::MAX should trigger an OOM!")
+ }
}
}
-
{
let mut ten_bytes: Vec<u8> = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
}
if guards_against_isize {
if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_CAP - 9) {
- } else { panic!("isize::MAX + 1 should trigger an overflow!"); }
+ } else {
+ panic!("isize::MAX + 1 should trigger an overflow!");
+ }
} else {
if let Err(AllocError { .. }) = ten_bytes.try_reserve_exact(MAX_CAP - 9) {
- } else { panic!("isize::MAX + 1 should trigger an OOM!") }
+ } else {
+ panic!("isize::MAX + 1 should trigger an OOM!")
+ }
}
if let Err(CapacityOverflow) = ten_bytes.try_reserve_exact(MAX_USIZE) {
- } else { panic!("usize::MAX should trigger an overflow!") }
+ } else {
+ panic!("usize::MAX should trigger an overflow!")
+ }
}
-
{
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) {
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) {
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) {
- } else { panic!("isize::MAX + 1 should trigger an overflow!"); }
+ if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9) {
+ } else {
+ panic!("isize::MAX + 1 should trigger an overflow!");
+ }
} else {
- if let Err(AllocError { .. }) = ten_u32s.try_reserve_exact(MAX_CAP/4 - 9) {
- } else { panic!("isize::MAX + 1 should trigger an OOM!") }
+ if let Err(AllocError { .. }) = ten_u32s.try_reserve_exact(MAX_CAP / 4 - 9) {
+ } else {
+ panic!("isize::MAX + 1 should trigger an OOM!")
+ }
}
if let Err(CapacityOverflow) = ten_u32s.try_reserve_exact(MAX_USIZE - 20) {
- } else { panic!("usize::MAX should trigger an overflow!") }
+ } else {
+ panic!("usize::MAX should trigger an overflow!")
+ }
}
-
}
#[test]
// Polyfill for https://github.com/rust-lang/rfcs/pull/2580
fn ptr_metadata(ptr: *mut dyn Fn()) -> *mut () {
- unsafe {
- std::mem::transmute::<*mut dyn Fn(), DynRepr>(ptr).vtable
- }
+ unsafe { std::mem::transmute::<*mut dyn Fn(), DynRepr>(ptr).vtable }
}
- fn ptr_from_raw_parts(data: *mut (), vtable: *mut()) -> *mut dyn Fn() {
- unsafe {
- std::mem::transmute::<DynRepr, *mut dyn Fn()>(DynRepr {
- data,
- vtable
- })
- }
+ fn ptr_from_raw_parts(data: *mut (), vtable: *mut ()) -> *mut dyn Fn() {
+ unsafe { std::mem::transmute::<DynRepr, *mut dyn Fn()>(DynRepr { data, vtable }) }
}
#[repr(C)]