From ecd5da5b0cae5b3af95f5b86a8157a1f57a944c5 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 1 Aug 2018 11:58:19 +0300 Subject: Introduce drop-bomb --- src/drop_bomb.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 src/drop_bomb.rs (limited to 'src/drop_bomb.rs') 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 @@ +use std::borrow::Cow; + +pub struct DropBomb { + msg: Cow<'static, str>, + defused: bool, +} + +impl DropBomb { + pub fn new(msg: impl Into>) -> DropBomb { + DropBomb { msg: msg.into(), defused: false } + } + pub fn defuse(&mut self) { self.defused = true } +} + +impl Drop for DropBomb { + fn drop(&mut self) { + if !self.defused && !::std::thread::panicking() { + panic!("{}", self.msg) + } + } +} -- cgit v1.2.3