From 433e4a3cdbde030e137c54887de78c322bc8c3e9 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 9 May 2022 15:14:00 +0200 Subject: [PATCH] remove unsafe from Droppable test helper --- src/binary_heap.rs | 8 ++++---- src/deque.rs | 35 ++++++----------------------------- src/test_helpers.rs | 18 +++++++++--------- src/vec.rs | 10 +++++----- 4 files changed, 24 insertions(+), 47 deletions(-) diff --git a/src/binary_heap.rs b/src/binary_heap.rs index 5bf01827..408008c5 100644 --- a/src/binary_heap.rs +++ b/src/binary_heap.rs @@ -582,7 +582,7 @@ mod tests { v.pop().unwrap(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); { let mut v: BinaryHeap = BinaryHeap::new(); @@ -590,7 +590,7 @@ mod tests { v.push(Droppable::new()).ok().unwrap(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); { let mut v: BinaryHeap = BinaryHeap::new(); @@ -599,7 +599,7 @@ mod tests { v.pop().unwrap(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); { let mut v: BinaryHeap = BinaryHeap::new(); @@ -607,7 +607,7 @@ mod tests { v.push(Droppable::new()).ok().unwrap(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); } #[test] diff --git a/src/deque.rs b/src/deque.rs index 65776212..6f3dbaf5 100644 --- a/src/deque.rs +++ b/src/deque.rs @@ -565,29 +565,6 @@ mod tests { let mut _v: Deque = Deque::new(); } - macro_rules! droppable { - () => { - struct Droppable; - impl Droppable { - fn new() -> Self { - unsafe { - COUNT += 1; - } - Droppable - } - } - impl Drop for Droppable { - fn drop(&mut self) { - unsafe { - COUNT -= 1; - } - } - } - - static mut COUNT: i32 = 0; - }; - } - #[test] fn drop() { droppable!(); @@ -599,7 +576,7 @@ mod tests { v.pop_front().unwrap(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); { let mut v: Deque = Deque::new(); @@ -607,14 +584,14 @@ mod tests { v.push_back(Droppable::new()).ok().unwrap(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); { let mut v: Deque = Deque::new(); v.push_front(Droppable::new()).ok().unwrap(); v.push_front(Droppable::new()).ok().unwrap(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); } #[test] @@ -754,7 +731,7 @@ mod tests { let _ = items.next(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); { let mut deque: Deque = Deque::new(); @@ -764,7 +741,7 @@ mod tests { // Move none } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); { let mut deque: Deque = Deque::new(); @@ -774,7 +751,7 @@ mod tests { let _ = items.next(); // Move partly } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); } #[test] diff --git a/src/test_helpers.rs b/src/test_helpers.rs index 8967f3fb..35956ab5 100644 --- a/src/test_helpers.rs +++ b/src/test_helpers.rs @@ -1,23 +1,23 @@ macro_rules! droppable { () => { + static COUNT: core::sync::atomic::AtomicI32 = core::sync::atomic::AtomicI32::new(0); + #[derive(Eq, Ord, PartialEq, PartialOrd)] struct Droppable(i32); impl Droppable { fn new() -> Self { - unsafe { - COUNT += 1; - Droppable(COUNT) - } + COUNT.fetch_add(1, core::sync::atomic::Ordering::Relaxed); + Droppable(Self::count()) + } + + fn count() -> i32 { + COUNT.load(core::sync::atomic::Ordering::Relaxed) } } impl Drop for Droppable { fn drop(&mut self) { - unsafe { - COUNT -= 1; - } + COUNT.fetch_sub(1, core::sync::atomic::Ordering::Relaxed); } } - - static mut COUNT: i32 = 0; }; } diff --git a/src/vec.rs b/src/vec.rs index 909e69d6..31d98ca7 100644 --- a/src/vec.rs +++ b/src/vec.rs @@ -912,7 +912,7 @@ mod tests { v.pop().unwrap(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); { let mut v: Vec = Vec::new(); @@ -920,7 +920,7 @@ mod tests { v.push(Droppable::new()).ok().unwrap(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); } #[test] @@ -1055,7 +1055,7 @@ mod tests { let _ = items.next(); } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); { let mut vec: Vec = Vec::new(); @@ -1065,7 +1065,7 @@ mod tests { // Move none } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); { let mut vec: Vec = Vec::new(); @@ -1075,7 +1075,7 @@ mod tests { let _ = items.next(); // Move partly } - assert_eq!(unsafe { COUNT }, 0); + assert_eq!(Droppable::count(), 0); } #[test]