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()();
}
}
|