kayrx_karx/executor/
utils.rs1use std::time::Instant;
2
3use lazy_static::lazy_static;
4
5pub fn abort_on_panic(f: impl FnOnce()) {
6 struct Bomb;
7
8 impl Drop for Bomb {
9 fn drop(&mut self) {
10 std::process::abort();
11 }
12 }
13
14 let bomb = Bomb;
15 f();
16 std::mem::forget(bomb);
17}
18
19macro_rules! defer {
20 ($($body:tt)*) => {
21 let _guard = {
22 pub struct Guard<F: FnOnce()>(Option<F>);
23
24 impl<F: FnOnce()> Drop for Guard<F> {
25 fn drop(&mut self) {
26 (self.0).take().map(|f| f());
27 }
28 }
29
30 Guard(Some(|| {
31 let _ = { $($body)* };
32 }))
33 };
34 };
35}
36
37pub fn monotonic_ms() -> u64 {
38 lazy_static! {
39 static ref START: Instant = Instant::now();
40 }
41 let start = *START;
42 Instant::now().duration_since(start).as_millis() as u64
43}