use std::cell::RefCell;
use std::future::Future;
use actix_rt::{System, SystemRunner};
use actix_service::Service;
use futures::future::{lazy, FutureExt};
thread_local! {
static RT: RefCell<Inner> = {
RefCell::new(Inner(Some(System::builder().build())))
};
}
struct Inner(Option<SystemRunner>);
impl Inner {
fn get_mut(&mut self) -> &mut SystemRunner {
self.0.as_mut().unwrap()
}
}
impl Drop for Inner {
fn drop(&mut self) {
std::mem::forget(self.0.take().unwrap())
}
}
pub fn block_on<F>(f: F) -> F::Output
where
F: Future,
{
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(f))
}
pub fn block_fn<F, R>(f: F) -> R::Output
where
F: FnOnce() -> R,
R: Future,
{
RT.with(move |rt| {
let mut rt = rt.borrow_mut();
let fut = rt.get_mut().block_on(lazy(|_| f()));
rt.get_mut().block_on(fut)
})
}
pub fn spawn<F>(fut: F)
where
F: Future + 'static,
{
run_on(move || {
actix_rt::spawn(fut.map(|_| ()));
});
}
pub fn run_on<F, R>(f: F) -> R
where
F: FnOnce() -> R,
{
RT.with(move |rt| rt.borrow_mut().get_mut().block_on(lazy(|_| f())))
}
pub fn call_service<S, R>(app: &mut S, req: R) -> S::Response
where
S: Service<Request = R>,
S::Error: std::fmt::Debug,
{
block_on(run_on(move || app.call(req))).unwrap()
}