aboutsummaryrefslogtreecommitdiff
path: root/src/drop_bomb.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-01 09:58:19 +0100
committerAleksey Kladov <[email protected]>2018-08-01 09:58:19 +0100
commitecd5da5b0cae5b3af95f5b86a8157a1f57a944c5 (patch)
treeeb14d6c340e2ee96666c90637c1ca3fc7ba1b5ea /src/drop_bomb.rs
parent37e1625f0139da07c2690b6ee6ca7eae5ebb061a (diff)
Introduce drop-bomb
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}