Skip to content

Commit 5bd918f

Browse files
committed
vec_deque tests: remove static mut
1 parent 1bb3352 commit 5bd918f

File tree

4 files changed

+31
-43
lines changed

4 files changed

+31
-43
lines changed

library/alloc/src/collections/linked_list/tests.rs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
use std::cell::Cell;
21
use std::panic::{AssertUnwindSafe, catch_unwind};
32
use std::thread;
43

54
use rand::RngCore;
65

76
use super::*;
87
use crate::testing::crash_test::{CrashTestDummy, Panic};
8+
use crate::testing::macros::struct_with_counted_drop;
99
use crate::vec::Vec;
1010

1111
#[test]
@@ -1010,22 +1010,6 @@ fn extract_if_drop_panic_leak() {
10101010
assert_eq!(d7.dropped(), 1);
10111011
}
10121012

1013-
macro_rules! struct_with_counted_drop {
1014-
($struct_name:ident$(($elt_ty:ty))?, $drop_counter:ident $(=> $drop_stmt:expr)?) => {
1015-
thread_local! {static $drop_counter: Cell<u32> = Cell::new(0);}
1016-
1017-
struct $struct_name$(($elt_ty))?;
1018-
1019-
impl Drop for $struct_name {
1020-
fn drop(&mut self) {
1021-
$drop_counter.set($drop_counter.get() + 1);
1022-
1023-
$($drop_stmt(self))?
1024-
}
1025-
}
1026-
};
1027-
}
1028-
10291013
#[test]
10301014
#[cfg_attr(not(panic = "unwind"), ignore = "test requires unwinding support")]
10311015
fn extract_if_pred_panic_leak() {

library/alloc/src/collections/vec_deque/tests.rs

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
2-
#![allow(static_mut_refs)]
3-
41
use core::iter::TrustedLen;
52

63
use super::*;
4+
use crate::testing::macros::struct_with_counted_drop;
75

86
#[bench]
97
fn bench_push_back_100(b: &mut test::Bencher) {
@@ -1086,36 +1084,24 @@ fn test_clone_from() {
10861084

10871085
#[test]
10881086
fn test_vec_deque_truncate_drop() {
1089-
static mut DROPS: u32 = 0;
1090-
#[derive(Clone)]
1091-
struct Elem(#[allow(dead_code)] i32);
1092-
impl Drop for Elem {
1093-
fn drop(&mut self) {
1094-
unsafe {
1095-
DROPS += 1;
1096-
}
1097-
}
1098-
}
1087+
struct_with_counted_drop!(Elem, DROPS);
10991088

1100-
let v = vec![Elem(1), Elem(2), Elem(3), Elem(4), Elem(5)];
1101-
for push_front in 0..=v.len() {
1102-
let v = v.clone();
1103-
let mut tester = VecDeque::with_capacity(5);
1104-
for (index, elem) in v.into_iter().enumerate() {
1089+
const LEN: usize = 5;
1090+
for push_front in 0..=LEN {
1091+
let mut tester = VecDeque::with_capacity(LEN);
1092+
for index in 0..LEN {
11051093
if index < push_front {
1106-
tester.push_front(elem);
1094+
tester.push_front(Elem);
11071095
} else {
1108-
tester.push_back(elem);
1096+
tester.push_back(Elem);
11091097
}
11101098
}
1111-
assert_eq!(unsafe { DROPS }, 0);
1099+
assert_eq!(DROPS.get(), 0);
11121100
tester.truncate(3);
1113-
assert_eq!(unsafe { DROPS }, 2);
1101+
assert_eq!(DROPS.get(), 2);
11141102
tester.truncate(0);
1115-
assert_eq!(unsafe { DROPS }, 5);
1116-
unsafe {
1117-
DROPS = 0;
1118-
}
1103+
assert_eq!(DROPS.get(), 5);
1104+
DROPS.set(0);
11191105
}
11201106
}
11211107

library/alloctests/testing/macros.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
macro_rules! struct_with_counted_drop {
2+
($struct_name:ident$(($elt_ty:ty))?, $drop_counter:ident $(=> $drop_stmt:expr)?) => {
3+
thread_local! {static $drop_counter: ::core::cell::Cell<u32> = ::core::cell::Cell::new(0);}
4+
5+
struct $struct_name$(($elt_ty))?;
6+
7+
impl ::std::ops::Drop for $struct_name {
8+
fn drop(&mut self) {
9+
$drop_counter.set($drop_counter.get() + 1);
10+
11+
$($drop_stmt(self))?
12+
}
13+
}
14+
};
15+
}
16+
17+
pub(crate) use struct_with_counted_drop;

library/alloctests/testing/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub(crate) mod crash_test;
2+
pub(crate) mod macros;
23
pub(crate) mod ord_chaos;
34
pub(crate) mod rng;

0 commit comments

Comments
 (0)