]> git.proxmox.com Git - rustc.git/blob - vendor/anyhow/tests/drop/mod.rs
ba2cc5ed68c4b93657b9b3156efd29ef2eafc3c7
[rustc.git] / vendor / anyhow / tests / drop / mod.rs
1 #![allow(clippy::module_name_repetitions)]
2
3 use std::error::Error as StdError;
4 use std::fmt::{self, Display};
5 use std::sync::atomic::AtomicBool;
6 use std::sync::atomic::Ordering::SeqCst;
7 use std::sync::Arc;
8
9 #[derive(Debug)]
10 pub struct Flag {
11 atomic: Arc<AtomicBool>,
12 }
13
14 impl Flag {
15 pub fn new() -> Self {
16 Flag {
17 atomic: Arc::new(AtomicBool::new(false)),
18 }
19 }
20
21 pub fn get(&self) -> bool {
22 self.atomic.load(SeqCst)
23 }
24 }
25
26 #[derive(Debug)]
27 pub struct DetectDrop {
28 has_dropped: Flag,
29 }
30
31 impl DetectDrop {
32 pub fn new(has_dropped: &Flag) -> Self {
33 DetectDrop {
34 has_dropped: Flag {
35 atomic: Arc::clone(&has_dropped.atomic),
36 },
37 }
38 }
39 }
40
41 impl StdError for DetectDrop {}
42
43 impl Display for DetectDrop {
44 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45 write!(f, "oh no!")
46 }
47 }
48
49 impl Drop for DetectDrop {
50 fn drop(&mut self) {
51 let already_dropped = self.has_dropped.atomic.swap(true, SeqCst);
52 assert!(!already_dropped);
53 }
54 }