use crate::{Clock, Duty, EmbeddedHost, HostConfig, Waiter, WakeHandle};
use super::{
ReactorControl, ReactorExit, ReactorHandle, ReactorSpawnError,
control::{ReactorControlOwner, control_pair},
handle, runner,
};
#[derive(Debug)]
pub struct Reactor<D, C, W> {
host: EmbeddedHost<D, C>,
waiter: W,
control: ReactorControlOwner,
}
impl<D, C, W> Reactor<D, C, W>
where
D: Duty,
C: Clock,
W: Waiter<D>,
{
pub fn new(duty: D, clock: C, waiter: W, termination_wake: WakeHandle) -> Self {
Self::with_config(duty, clock, waiter, termination_wake, HostConfig::default())
}
pub fn with_config(
duty: D,
clock: C,
waiter: W,
termination_wake: WakeHandle,
config: HostConfig,
) -> Self {
Self::from_host(
EmbeddedHost::new(duty, clock, config),
waiter,
termination_wake,
)
}
pub fn from_host(host: EmbeddedHost<D, C>, waiter: W, termination_wake: WakeHandle) -> Self {
let (control, _external) = control_pair(termination_wake);
Self {
host,
waiter,
control,
}
}
pub fn run(self) -> ReactorExit<D, C, W> {
runner::run(self.host, self.waiter, &self.control)
}
pub fn spawn(
self,
name: impl Into<String>,
) -> Result<ReactorHandle<D, C, W>, ReactorSpawnError<D, C, W>>
where
D: Send + 'static,
D::Error: Send + 'static,
C: Send + 'static,
C::Error: Send + 'static,
W: Send + 'static,
W::Error: Send + 'static,
{
handle::spawn(name.into(), self)
}
pub const fn duty(&self) -> &D {
self.host.duty()
}
pub fn duty_mut(&mut self) -> &mut D {
self.host.duty_mut()
}
pub const fn waiter(&self) -> &W {
&self.waiter
}
pub fn waiter_mut(&mut self) -> &mut W {
&mut self.waiter
}
pub fn into_parts(self) -> (EmbeddedHost<D, C>, W) {
(self.host, self.waiter)
}
pub(crate) fn control(&self) -> ReactorControl {
self.control.control()
}
}