use std::future::Future;
use std::io;
use tokio::{runtime, task::LocalSet};
#[derive(Debug)]
pub struct Runtime {
local: LocalSet,
rt: runtime::Runtime,
}
impl Runtime {
#[allow(clippy::new_ret_no_self)]
pub fn new() -> io::Result<Runtime> {
let rt = runtime::Builder::new()
.enable_io()
.enable_time()
.basic_scheduler()
.build()?;
Ok(Runtime {
rt,
local: LocalSet::new(),
})
}
pub fn spawn<F>(&self, future: F) -> &Self
where
F: Future<Output = ()> + 'static,
{
self.local.spawn_local(future);
self
}
pub fn block_on<F>(&mut self, f: F) -> F::Output
where
F: Future + 'static,
{
let res = self.local.block_on(&mut self.rt, f);
res
}
}