use std::marker::PhantomData;
use zrx_storage::set::{View, ViewMut};
use crate::scheduler::signal::Id;
use crate::scheduler::step::Scoped;
use super::{Context, Event};
mod error;
pub use error::{Error, Result};
#[derive(Debug)]
pub struct Builder<'a, I> {
events: Vec<Event<I>>,
inputs: Option<View<'a>>,
output: Option<ViewMut<'a>>,
marker: PhantomData<I>,
}
impl<'a, I> Context<'a, I> {
#[inline]
#[must_use]
pub fn builder() -> Builder<'a, I> {
Builder::default()
}
}
impl<'a, I> Builder<'a, I>
where
I: Id,
{
#[must_use]
pub fn events(mut self, events: Vec<Event<I>>) -> Self {
self.events = events;
self
}
#[must_use]
pub fn inputs(mut self, view: View<'a>) -> Self {
self.inputs = Some(view);
self
}
#[must_use]
pub fn output(mut self, view: ViewMut<'a>) -> Self {
self.output = Some(view);
self
}
pub fn build<T, C>(self, scopes: T) -> Result<Context<'a, I, C>>
where
T: IntoIterator<Item = Scoped<I>>,
{
Ok(Context {
events: self.events,
scopes: scopes.into_iter().collect(),
inputs: self.inputs.ok_or(Error::Inputs)?,
output: self.output.ok_or(Error::Output)?,
marker: PhantomData,
})
}
}
impl<I> Default for Builder<'_, I> {
#[inline]
fn default() -> Self {
Self {
events: Vec::default(),
inputs: None,
output: None,
marker: PhantomData,
}
}
}