diff options
Diffstat (limited to 'src/drop_bomb.rs')
-rw-r--r-- | src/drop_bomb.rs | 21 |
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 @@ | |||
1 | use std::borrow::Cow; | ||
2 | |||
3 | pub struct DropBomb { | ||
4 | msg: Cow<'static, str>, | ||
5 | defused: bool, | ||
6 | } | ||
7 | |||
8 | impl 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 | |||
15 | impl Drop for DropBomb { | ||
16 | fn drop(&mut self) { | ||
17 | if !self.defused && !::std::thread::panicking() { | ||
18 | panic!("{}", self.msg) | ||
19 | } | ||
20 | } | ||
21 | } | ||