ntex_rt/abort.rs
1/// Aborts the process when dropped, logging the message first.
2///
3/// Guards code that must not unwind, for example while state that the kernel
4/// or other threads still use is taken out of its owner. Declare the guard
5/// after such state, so it is dropped first while unwinding, and call
6/// [`disarm()`](Self::disarm) once the guarded code completed.
7///
8/// Bind the guard to a named variable, `let _ = AbortOnUnwind(..)` drops it,
9/// and aborts, immediately.
10///
11/// ```rust,no_run
12/// use ntex_rt::AbortOnUnwind;
13///
14/// let guard = AbortOnUnwind("operation panicked");
15/// // code that must not unwind
16/// guard.disarm();
17/// ```
18#[derive(Debug)]
19#[must_use = "the guard aborts the process when dropped"]
20pub struct AbortOnUnwind(pub &'static str);
21
22impl AbortOnUnwind {
23 /// Releases the guard without aborting.
24 pub fn disarm(self) {
25 std::mem::forget(self);
26 }
27}
28
29impl Drop for AbortOnUnwind {
30 fn drop(&mut self) {
31 log::error!("{}; aborting", self.0);
32 std::process::abort();
33 }
34}