Skip to content

Commit 56d2a37

Browse files
authored
Merge pull request #288 from japaric/safe-droppable
remove unsafe from Droppable test helper
2 parents 44fb37d + 433e4a3 commit 56d2a37

File tree

4 files changed

+24
-47
lines changed

4 files changed

+24
-47
lines changed

src/binary_heap.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -582,15 +582,15 @@ mod tests {
582582
v.pop().unwrap();
583583
}
584584

585-
assert_eq!(unsafe { COUNT }, 0);
585+
assert_eq!(Droppable::count(), 0);
586586

587587
{
588588
let mut v: BinaryHeap<Droppable, Max, 2> = BinaryHeap::new();
589589
v.push(Droppable::new()).ok().unwrap();
590590
v.push(Droppable::new()).ok().unwrap();
591591
}
592592

593-
assert_eq!(unsafe { COUNT }, 0);
593+
assert_eq!(Droppable::count(), 0);
594594

595595
{
596596
let mut v: BinaryHeap<Droppable, Min, 2> = BinaryHeap::new();
@@ -599,15 +599,15 @@ mod tests {
599599
v.pop().unwrap();
600600
}
601601

602-
assert_eq!(unsafe { COUNT }, 0);
602+
assert_eq!(Droppable::count(), 0);
603603

604604
{
605605
let mut v: BinaryHeap<Droppable, Min, 2> = BinaryHeap::new();
606606
v.push(Droppable::new()).ok().unwrap();
607607
v.push(Droppable::new()).ok().unwrap();
608608
}
609609

610-
assert_eq!(unsafe { COUNT }, 0);
610+
assert_eq!(Droppable::count(), 0);
611611
}
612612

613613
#[test]

src/deque.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -565,29 +565,6 @@ mod tests {
565565
let mut _v: Deque<i32, 4> = Deque::new();
566566
}
567567

568-
macro_rules! droppable {
569-
() => {
570-
struct Droppable;
571-
impl Droppable {
572-
fn new() -> Self {
573-
unsafe {
574-
COUNT += 1;
575-
}
576-
Droppable
577-
}
578-
}
579-
impl Drop for Droppable {
580-
fn drop(&mut self) {
581-
unsafe {
582-
COUNT -= 1;
583-
}
584-
}
585-
}
586-
587-
static mut COUNT: i32 = 0;
588-
};
589-
}
590-
591568
#[test]
592569
fn drop() {
593570
droppable!();
@@ -599,22 +576,22 @@ mod tests {
599576
v.pop_front().unwrap();
600577
}
601578

602-
assert_eq!(unsafe { COUNT }, 0);
579+
assert_eq!(Droppable::count(), 0);
603580

604581
{
605582
let mut v: Deque<Droppable, 2> = Deque::new();
606583
v.push_back(Droppable::new()).ok().unwrap();
607584
v.push_back(Droppable::new()).ok().unwrap();
608585
}
609586

610-
assert_eq!(unsafe { COUNT }, 0);
587+
assert_eq!(Droppable::count(), 0);
611588
{
612589
let mut v: Deque<Droppable, 2> = Deque::new();
613590
v.push_front(Droppable::new()).ok().unwrap();
614591
v.push_front(Droppable::new()).ok().unwrap();
615592
}
616593

617-
assert_eq!(unsafe { COUNT }, 0);
594+
assert_eq!(Droppable::count(), 0);
618595
}
619596

620597
#[test]
@@ -754,7 +731,7 @@ mod tests {
754731
let _ = items.next();
755732
}
756733

757-
assert_eq!(unsafe { COUNT }, 0);
734+
assert_eq!(Droppable::count(), 0);
758735

759736
{
760737
let mut deque: Deque<Droppable, 2> = Deque::new();
@@ -764,7 +741,7 @@ mod tests {
764741
// Move none
765742
}
766743

767-
assert_eq!(unsafe { COUNT }, 0);
744+
assert_eq!(Droppable::count(), 0);
768745

769746
{
770747
let mut deque: Deque<Droppable, 2> = Deque::new();
@@ -774,7 +751,7 @@ mod tests {
774751
let _ = items.next(); // Move partly
775752
}
776753

777-
assert_eq!(unsafe { COUNT }, 0);
754+
assert_eq!(Droppable::count(), 0);
778755
}
779756

780757
#[test]

src/test_helpers.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
macro_rules! droppable {
22
() => {
3+
static COUNT: core::sync::atomic::AtomicI32 = core::sync::atomic::AtomicI32::new(0);
4+
35
#[derive(Eq, Ord, PartialEq, PartialOrd)]
46
struct Droppable(i32);
57
impl Droppable {
68
fn new() -> Self {
7-
unsafe {
8-
COUNT += 1;
9-
Droppable(COUNT)
10-
}
9+
COUNT.fetch_add(1, core::sync::atomic::Ordering::Relaxed);
10+
Droppable(Self::count())
11+
}
12+
13+
fn count() -> i32 {
14+
COUNT.load(core::sync::atomic::Ordering::Relaxed)
1115
}
1216
}
1317
impl Drop for Droppable {
1418
fn drop(&mut self) {
15-
unsafe {
16-
COUNT -= 1;
17-
}
19+
COUNT.fetch_sub(1, core::sync::atomic::Ordering::Relaxed);
1820
}
1921
}
20-
21-
static mut COUNT: i32 = 0;
2222
};
2323
}

src/vec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -912,15 +912,15 @@ mod tests {
912912
v.pop().unwrap();
913913
}
914914

915-
assert_eq!(unsafe { COUNT }, 0);
915+
assert_eq!(Droppable::count(), 0);
916916

917917
{
918918
let mut v: Vec<Droppable, 2> = Vec::new();
919919
v.push(Droppable::new()).ok().unwrap();
920920
v.push(Droppable::new()).ok().unwrap();
921921
}
922922

923-
assert_eq!(unsafe { COUNT }, 0);
923+
assert_eq!(Droppable::count(), 0);
924924
}
925925

926926
#[test]
@@ -1055,7 +1055,7 @@ mod tests {
10551055
let _ = items.next();
10561056
}
10571057

1058-
assert_eq!(unsafe { COUNT }, 0);
1058+
assert_eq!(Droppable::count(), 0);
10591059

10601060
{
10611061
let mut vec: Vec<Droppable, 2> = Vec::new();
@@ -1065,7 +1065,7 @@ mod tests {
10651065
// Move none
10661066
}
10671067

1068-
assert_eq!(unsafe { COUNT }, 0);
1068+
assert_eq!(Droppable::count(), 0);
10691069

10701070
{
10711071
let mut vec: Vec<Droppable, 2> = Vec::new();
@@ -1075,7 +1075,7 @@ mod tests {
10751075
let _ = items.next(); // Move partly
10761076
}
10771077

1078-
assert_eq!(unsafe { COUNT }, 0);
1078+
assert_eq!(Droppable::count(), 0);
10791079
}
10801080

10811081
#[test]

0 commit comments

Comments
 (0)