aboutsummaryrefslogtreecommitdiff
path: root/src/drop_bomb.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/drop_bomb.rs')
-rw-r--r--src/drop_bomb.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/drop_bomb.rs b/src/drop_bomb.rs
new file mode 100644
index 000000000..750904a01
--- /dev/null
+++ b/src/drop_bomb.rs
@@ -0,0 +1,21 @@
1use std::borrow::Cow;
2
3pub struct DropBomb {
4 msg: Cow<'static, str>,
5 defused: bool,
6}
7
8impl DropBomb {
9 pub fn new(msg: impl Into<Cow<'static, str>>) -> DropBomb {
10 DropBomb { msg: msg.into(), defused: false }
11 }
12 pub fn defuse(&mut self) { self.defused = true }
13}
14
15impl Drop for DropBomb {
16 fn drop(&mut self) {
17 if !self.defused && !::std::thread::panicking() {
18 panic!("{}", self.msg)
19 }
20 }
21}