2021-06-27 22:07:33 +08:00

54 lines
1.2 KiB
Rust

use std::{future::Future, io, thread};
use thread_id;
use tokio::{runtime, task::LocalSet};
#[derive(Debug)]
pub struct Runtime {
local: LocalSet,
rt: runtime::Runtime,
}
impl Runtime {
pub fn new() -> io::Result<Runtime> {
let rt = runtime::Builder::new_multi_thread()
.thread_name("flowy-sys")
.enable_io()
.enable_time()
.on_thread_start(move || {
log::trace!(
"{:?} thread started: thread_id= {}",
thread::current(),
thread_id::get()
);
})
.on_thread_stop(move || {
log::trace!(
"{:?} thread stopping: thread_id= {}",
thread::current(),
thread_id::get(),
);
})
.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>(&self, f: F) -> F::Output
where
F: Future + 'static,
{
self.local.block_on(&self.rt, f)
}
}