use crate::fiber::{block_pool, Spawner, io, timer};
use crate::fiber::JoinHandle;
use std::future::Future;
#[derive(Debug, Clone)]
pub struct Handle {
pub(super) spawner: Spawner,
pub(super) io_handle: io::Handle,
pub(super) timer_handle: timer::Handle,
pub(super) clock: timer::Clock,
pub(super) blocking_spawner: block_pool::Spawner,
}
impl Handle {
pub fn enter<F, R>(&self, f: F) -> R
where
F: FnOnce() -> R,
{
self.blocking_spawner.enter(|| {
let _io = io::set_default(&self.io_handle);
timer::with_default(&self.timer_handle, &self.clock, || self.spawner.enter(f))
})
}
pub fn spawn<F>(&self, future: F) -> JoinHandle<F::Output>
where
F: Future + Send + 'static,
F::Output: Send + 'static,
{
self.spawner.spawn(future)
}
}