File: mod.rs

package info (click to toggle)
rust-async-backtrace 0.2.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252 kB
  • sloc: makefile: 2
file content (51 lines) | stat: -rw-r--r-- 1,192 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#![allow(unused_imports, unused_variables, dead_code)]

use std::{future::Future, sync::Mutex, task::Poll};

pub(crate) fn model<F>(f: F)
where
    F: Fn() + Sync + Send + 'static,
{
    #[cfg(not(loom))]
    f();
    #[cfg(loom)]
    loom::model(f);
}

pub(crate) mod thread {
    #[cfg(not(loom))]
    pub(crate) use std::thread::{spawn, yield_now};

    #[cfg(loom)]
    pub(crate) use loom::thread::{spawn, yield_now};
}

pub fn run<F: Future>(f: F) -> <F as Future>::Output {
    use std::task::Context;
    let mut f = Box::pin(f);
    let waker = futures::task::noop_waker();
    let mut cx = Context::from_waker(&waker);
    loop {
        match f.as_mut().poll(&mut cx) {
            Poll::Ready(v) => return v,
            Poll::Pending => thread::yield_now(),
        }
    }
}

pub fn strip(str: impl AsRef<str>) -> String {
    let re = regex::Regex::new(r":\d+:\d+").unwrap();
    re.replace_all(str.as_ref(), ":LINE:COL").to_string()
}

pub fn defer<F: FnOnce() -> R, R>(f: F) -> impl Drop {
    Defer(Some(f))
}

struct Defer<F: FnOnce() -> R, R>(Option<F>);

impl<F: FnOnce() -> R, R> Drop for Defer<F, R> {
    fn drop(&mut self) {
        self.0.take().unwrap()();
    }
}