| 1 | /// Type which will panic if dropped. |
| 2 | /// |
| 3 | /// If this is dropped during a panic, this will cause an abort. |
| 4 | /// |
| 5 | /// Use this to avoid letting unwinds cross through the FFI boundary, which is UB. |
| 6 | pub struct PanicTrap { |
| 7 | msg: &'static str, |
| 8 | } |
| 9 | |
| 10 | impl PanicTrap { |
| 11 | #[inline ] |
| 12 | pub const fn new(msg: &'static str) -> Self { |
| 13 | Self { msg } |
| 14 | } |
| 15 | |
| 16 | #[inline ] |
| 17 | pub const fn disarm(self) { |
| 18 | std::mem::forget(self) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | impl Drop for PanicTrap { |
| 23 | fn drop(&mut self) { |
| 24 | // Panic here will abort the process, assuming in an unwind. |
| 25 | panic!("{}" , self.msg) |
| 26 | } |
| 27 | } |
| 28 | |