Atomic flag type for use in Drop tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Tolnay · committed by David Tolnay · parent e674ddc · Browse files · 4 files·+32 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+32 added−22 removed
- tests:+32 added−22 removed
Provenance
- Commit
b2f0f45- Tree
de7d889- Author
David Tolnay <dtolnay@gmail.com>- Committer
David Tolnay <dtolnay@gmail.com>- Parent
e674ddc
4 files changed+32 added−22 removed
modified:tests/drop/mod.rs+23 added-4 removed
3 unmodified lines4 use std::sync::atomic::Ordering::SeqCst;5 use std::sync::Arc;6 7 #[derive(Debug)]8+pub struct Flag {9+ atomic: Arc<AtomicBool>,10+}11+12+impl Flag {13+ pub fn new() -> Self {14+ Flag {15+ atomic: Arc::new(AtomicBool::new(false)),16+ }17+ }18+19+ pub fn get(&self) -> bool {20+ self.atomic.load(SeqCst)21+ }22+}23+24+#[derive(Debug)]25 pub struct DetectDrop {9- has_dropped: Arc<AtomicBool>,26+ has_dropped: Flag,27 }28 29 impl DetectDrop {13- pub fn new(has_dropped: &Arc<AtomicBool>) -> Self {30+ pub fn new(has_dropped: &Flag) -> Self {31 DetectDrop {15- has_dropped: Arc::clone(has_dropped),32+ has_dropped: Flag {33+ atomic: Arc::clone(&has_dropped.atomic),34+ },35 }36 }37 }38 6 unmodified lines45 }46 47 impl Drop for DetectDrop {48 fn drop(&mut self) {30- let already_dropped = self.has_dropped.swap(true, SeqCst);49+ let already_dropped = self.has_dropped.atomic.swap(true, SeqCst);50 assert!(!already_dropped);51 }52 }modified:tests/test_convert.rs+3 added-6 removed
1 mod drop;2 3-use self::drop::DetectDrop;3+use self::drop::{DetectDrop, Flag};4 use anyhow::{Error, Result};5 use std::error::Error as StdError;6-use std::sync::atomic::AtomicBool;7-use std::sync::atomic::Ordering::SeqCst;8-use std::sync::Arc;6 7 #[test]8 fn test_convert() {12- let has_dropped = Arc::new(AtomicBool::new(false));9+ let has_dropped = Flag::new();10 let error = Error::new(DetectDrop::new(&has_dropped));11 let box_dyn = Box::<dyn StdError + Send + Sync>::from(error);12 assert_eq!("oh no!", box_dyn.to_string());13 drop(box_dyn);17- assert!(has_dropped.load(SeqCst));14+ assert!(has_dropped.get());15 }16 17 #[test]18 fn test_question_mark() -> Result<(), Box<dyn StdError>> {6 unmodified linesmodified:tests/test_downcast.rs+3 added-6 removed
1 mod common;2 mod drop;3 4 use self::common::*;5-use self::drop::DetectDrop;5+use self::drop::{DetectDrop, Flag};6 use anyhow::Error;7 use std::error::Error as StdError;8 use std::fmt::{self, Display};9 use std::io;10-use std::sync::atomic::AtomicBool;11-use std::sync::atomic::Ordering::SeqCst;12-use std::sync::Arc;10 11 #[test]12 fn test_downcast() {13 assert_eq!(55 unmodified lines69 }70 71 #[test]72 fn test_drop() {76- let has_dropped = Arc::new(AtomicBool::new(false));73+ let has_dropped = Flag::new();74 let error = Error::new(DetectDrop::new(&has_dropped));75 drop(error.downcast::<DetectDrop>().unwrap());79- assert!(has_dropped.load(SeqCst));76+ assert!(has_dropped.get());77 }78 79 #[test]80 fn test_large_alignment() {26 unmodified linesmodified:tests/test_repr.rs+3 added-6 removed
1 mod drop;2 3-use self::drop::DetectDrop;3+use self::drop::{DetectDrop, Flag};4 use anyhow::Error;5 use std::marker::Unpin;6 use std::mem;7-use std::sync::atomic::AtomicBool;8-use std::sync::atomic::Ordering::SeqCst;9-use std::sync::Arc;7 8 #[test]9 fn test_error_size() {10 assert_eq!(mem::size_of::<Error>(), mem::size_of::<usize>());11 unmodified lines22 }23 24 #[test]25 fn test_drop() {29- let has_dropped = Arc::new(AtomicBool::new(false));26+ let has_dropped = Flag::new();27 drop(Error::new(DetectDrop::new(&has_dropped)));31- assert!(has_dropped.load(SeqCst));28+ assert!(has_dropped.get());29 }