mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-09-30 22:42:02 +00:00
Atomic flag type for use in Drop tests
This commit is contained in:
parent
e674ddcf79
commit
b2f0f456f5
@ -4,15 +4,34 @@ use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering::SeqCst;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Flag {
|
||||
atomic: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl Flag {
|
||||
pub fn new() -> Self {
|
||||
Flag {
|
||||
atomic: Arc::new(AtomicBool::new(false)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self) -> bool {
|
||||
self.atomic.load(SeqCst)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DetectDrop {
|
||||
has_dropped: Arc<AtomicBool>,
|
||||
has_dropped: Flag,
|
||||
}
|
||||
|
||||
impl DetectDrop {
|
||||
pub fn new(has_dropped: &Arc<AtomicBool>) -> Self {
|
||||
pub fn new(has_dropped: &Flag) -> Self {
|
||||
DetectDrop {
|
||||
has_dropped: Arc::clone(has_dropped),
|
||||
has_dropped: Flag {
|
||||
atomic: Arc::clone(&has_dropped.atomic),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -27,7 +46,7 @@ impl Display for DetectDrop {
|
||||
|
||||
impl Drop for DetectDrop {
|
||||
fn drop(&mut self) {
|
||||
let already_dropped = self.has_dropped.swap(true, SeqCst);
|
||||
let already_dropped = self.has_dropped.atomic.swap(true, SeqCst);
|
||||
assert!(!already_dropped);
|
||||
}
|
||||
}
|
||||
|
@ -1,20 +1,17 @@
|
||||
mod drop;
|
||||
|
||||
use self::drop::DetectDrop;
|
||||
use self::drop::{DetectDrop, Flag};
|
||||
use anyhow::{Error, Result};
|
||||
use std::error::Error as StdError;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering::SeqCst;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[test]
|
||||
fn test_convert() {
|
||||
let has_dropped = Arc::new(AtomicBool::new(false));
|
||||
let has_dropped = Flag::new();
|
||||
let error = Error::new(DetectDrop::new(&has_dropped));
|
||||
let box_dyn = Box::<dyn StdError + Send + Sync>::from(error);
|
||||
assert_eq!("oh no!", box_dyn.to_string());
|
||||
drop(box_dyn);
|
||||
assert!(has_dropped.load(SeqCst));
|
||||
assert!(has_dropped.get());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -2,14 +2,11 @@ mod common;
|
||||
mod drop;
|
||||
|
||||
use self::common::*;
|
||||
use self::drop::DetectDrop;
|
||||
use self::drop::{DetectDrop, Flag};
|
||||
use anyhow::Error;
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt::{self, Display};
|
||||
use std::io;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering::SeqCst;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[test]
|
||||
fn test_downcast() {
|
||||
@ -73,10 +70,10 @@ fn test_downcast_mut() {
|
||||
|
||||
#[test]
|
||||
fn test_drop() {
|
||||
let has_dropped = Arc::new(AtomicBool::new(false));
|
||||
let has_dropped = Flag::new();
|
||||
let error = Error::new(DetectDrop::new(&has_dropped));
|
||||
drop(error.downcast::<DetectDrop>().unwrap());
|
||||
assert!(has_dropped.load(SeqCst));
|
||||
assert!(has_dropped.get());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -1,12 +1,9 @@
|
||||
mod drop;
|
||||
|
||||
use self::drop::DetectDrop;
|
||||
use self::drop::{DetectDrop, Flag};
|
||||
use anyhow::Error;
|
||||
use std::marker::Unpin;
|
||||
use std::mem;
|
||||
use std::sync::atomic::AtomicBool;
|
||||
use std::sync::atomic::Ordering::SeqCst;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[test]
|
||||
fn test_error_size() {
|
||||
@ -26,7 +23,7 @@ fn test_autotraits() {
|
||||
|
||||
#[test]
|
||||
fn test_drop() {
|
||||
let has_dropped = Arc::new(AtomicBool::new(false));
|
||||
let has_dropped = Flag::new();
|
||||
drop(Error::new(DetectDrop::new(&has_dropped)));
|
||||
assert!(has_dropped.load(SeqCst));
|
||||
assert!(has_dropped.get());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user