Skip to content
Myr

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

4 files+32 added−22 removed

modified:tests/drop/mod.rs+23 added-4 removed
3 unmodified lines
4use std::sync::atomic::Ordering::SeqCst;5use std::sync::Arc;67#[derive(Debug)]8pub struct Flag {9 atomic: Arc<AtomicBool>,10}1112impl Flag {13 pub fn new() -> Self {14 Flag {15 atomic: Arc::new(AtomicBool::new(false)),16 }17 }1819 pub fn get(&self) -> bool {20 self.atomic.load(SeqCst)21 }22}2324#[derive(Debug)]25pub struct DetectDrop {9 has_dropped: Arc<AtomicBool>,26 has_dropped: Flag,27}2829impl 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 lines
45}4647impl 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
1mod drop;23use self::drop::DetectDrop;3use self::drop::{DetectDrop, Flag};4use anyhow::{Error, Result};5use std::error::Error as StdError;6use std::sync::atomic::AtomicBool;7use std::sync::atomic::Ordering::SeqCst;8use std::sync::Arc;67#[test]8fn 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}1617#[test]18fn test_question_mark() -> Result<(), Box<dyn StdError>> {
6 unmodified lines
modified:tests/test_downcast.rs+3 added-6 removed
1mod common;2mod drop;34use self::common::*;5use self::drop::DetectDrop;5use self::drop::{DetectDrop, Flag};6use anyhow::Error;7use std::error::Error as StdError;8use std::fmt::{self, Display};9use std::io;10use std::sync::atomic::AtomicBool;11use std::sync::atomic::Ordering::SeqCst;12use std::sync::Arc;1011#[test]12fn test_downcast() {13 assert_eq!(
55 unmodified lines
69}7071#[test]72fn 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}7879#[test]80fn test_large_alignment() {
26 unmodified lines
modified:tests/test_repr.rs+3 added-6 removed
1mod drop;23use self::drop::DetectDrop;3use self::drop::{DetectDrop, Flag};4use anyhow::Error;5use std::marker::Unpin;6use std::mem;7use std::sync::atomic::AtomicBool;8use std::sync::atomic::Ordering::SeqCst;9use std::sync::Arc;78#[test]9fn test_error_size() {10 assert_eq!(mem::size_of::<Error>(), mem::size_of::<usize>());
11 unmodified lines
22}2324#[test]25fn 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}