use crate::{
schedule::{
AmbiguitySetLabel, BoxedAmbiguitySetLabel, BoxedSystemLabel, IntoRunCriteria,
RunCriteriaDescriptorOrLabel, SystemLabel,
},
system::{BoxedSystem, ExclusiveSystem, ExclusiveSystemCoerced, ExclusiveSystemFn, System},
};
pub enum SystemDescriptor {
Parallel(ParallelSystemDescriptor),
Exclusive(ExclusiveSystemDescriptor),
}
pub struct SystemLabelMarker;
impl From<ParallelSystemDescriptor> for SystemDescriptor {
fn from(descriptor: ParallelSystemDescriptor) -> Self {
SystemDescriptor::Parallel(descriptor)
}
}
impl<S> From<S> for SystemDescriptor
where
S: System<In = (), Out = ()>,
{
fn from(system: S) -> Self {
new_parallel_descriptor(Box::new(system)).into()
}
}
impl From<BoxedSystem<(), ()>> for SystemDescriptor {
fn from(system: BoxedSystem<(), ()>) -> Self {
new_parallel_descriptor(system).into()
}
}
impl From<ExclusiveSystemDescriptor> for SystemDescriptor {
fn from(descriptor: ExclusiveSystemDescriptor) -> Self {
SystemDescriptor::Exclusive(descriptor)
}
}
impl From<ExclusiveSystemFn> for SystemDescriptor {
fn from(system: ExclusiveSystemFn) -> Self {
new_exclusive_descriptor(Box::new(system)).into()
}
}
impl From<ExclusiveSystemCoerced> for SystemDescriptor {
fn from(system: ExclusiveSystemCoerced) -> Self {
new_exclusive_descriptor(Box::new(system)).into()
}
}
pub struct ParallelSystemDescriptor {
pub(crate) system: BoxedSystem<(), ()>,
pub(crate) run_criteria: Option<RunCriteriaDescriptorOrLabel>,
pub(crate) labels: Vec<BoxedSystemLabel>,
pub(crate) before: Vec<BoxedSystemLabel>,
pub(crate) after: Vec<BoxedSystemLabel>,
pub(crate) ambiguity_sets: Vec<BoxedAmbiguitySetLabel>,
}
fn new_parallel_descriptor(system: BoxedSystem<(), ()>) -> ParallelSystemDescriptor {
ParallelSystemDescriptor {
system,
run_criteria: None,
labels: Vec::new(),
before: Vec::new(),
after: Vec::new(),
ambiguity_sets: Vec::new(),
}
}
pub trait ParallelSystemDescriptorCoercion {
fn with_run_criteria<Marker>(
self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> ParallelSystemDescriptor;
fn label(self, label: impl SystemLabel) -> ParallelSystemDescriptor;
fn before(self, label: impl SystemLabel) -> ParallelSystemDescriptor;
fn after(self, label: impl SystemLabel) -> ParallelSystemDescriptor;
fn in_ambiguity_set(self, set: impl AmbiguitySetLabel) -> ParallelSystemDescriptor;
}
impl ParallelSystemDescriptorCoercion for ParallelSystemDescriptor {
fn with_run_criteria<Marker>(
mut self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> ParallelSystemDescriptor {
self.run_criteria = Some(run_criteria.into());
self
}
fn label(mut self, label: impl SystemLabel) -> ParallelSystemDescriptor {
self.labels.push(Box::new(label));
self
}
fn before(mut self, label: impl SystemLabel) -> ParallelSystemDescriptor {
self.before.push(Box::new(label));
self
}
fn after(mut self, label: impl SystemLabel) -> ParallelSystemDescriptor {
self.after.push(Box::new(label));
self
}
fn in_ambiguity_set(mut self, set: impl AmbiguitySetLabel) -> ParallelSystemDescriptor {
self.ambiguity_sets.push(Box::new(set));
self
}
}
impl<S> ParallelSystemDescriptorCoercion for S
where
S: System<In = (), Out = ()>,
{
fn with_run_criteria<Marker>(
self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> ParallelSystemDescriptor {
new_parallel_descriptor(Box::new(self)).with_run_criteria(run_criteria)
}
fn label(self, label: impl SystemLabel) -> ParallelSystemDescriptor {
new_parallel_descriptor(Box::new(self)).label(label)
}
fn before(self, label: impl SystemLabel) -> ParallelSystemDescriptor {
new_parallel_descriptor(Box::new(self)).before(label)
}
fn after(self, label: impl SystemLabel) -> ParallelSystemDescriptor {
new_parallel_descriptor(Box::new(self)).after(label)
}
fn in_ambiguity_set(self, set: impl AmbiguitySetLabel) -> ParallelSystemDescriptor {
new_parallel_descriptor(Box::new(self)).in_ambiguity_set(set)
}
}
impl ParallelSystemDescriptorCoercion for BoxedSystem<(), ()> {
fn with_run_criteria<Marker>(
self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> ParallelSystemDescriptor {
new_parallel_descriptor(self).with_run_criteria(run_criteria)
}
fn label(self, label: impl SystemLabel) -> ParallelSystemDescriptor {
new_parallel_descriptor(self).label(label)
}
fn before(self, label: impl SystemLabel) -> ParallelSystemDescriptor {
new_parallel_descriptor(self).before(label)
}
fn after(self, label: impl SystemLabel) -> ParallelSystemDescriptor {
new_parallel_descriptor(self).after(label)
}
fn in_ambiguity_set(self, set: impl AmbiguitySetLabel) -> ParallelSystemDescriptor {
new_parallel_descriptor(self).in_ambiguity_set(set)
}
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum InsertionPoint {
AtStart,
BeforeCommands,
AtEnd,
}
pub struct ExclusiveSystemDescriptor {
pub(crate) system: Box<dyn ExclusiveSystem>,
pub(crate) run_criteria: Option<RunCriteriaDescriptorOrLabel>,
pub(crate) labels: Vec<BoxedSystemLabel>,
pub(crate) before: Vec<BoxedSystemLabel>,
pub(crate) after: Vec<BoxedSystemLabel>,
pub(crate) ambiguity_sets: Vec<BoxedAmbiguitySetLabel>,
pub(crate) insertion_point: InsertionPoint,
}
fn new_exclusive_descriptor(system: Box<dyn ExclusiveSystem>) -> ExclusiveSystemDescriptor {
ExclusiveSystemDescriptor {
system,
run_criteria: None,
labels: Vec::new(),
before: Vec::new(),
after: Vec::new(),
ambiguity_sets: Vec::new(),
insertion_point: InsertionPoint::AtStart,
}
}
pub trait ExclusiveSystemDescriptorCoercion {
fn with_run_criteria<Marker>(
self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> ExclusiveSystemDescriptor;
fn label(self, label: impl SystemLabel) -> ExclusiveSystemDescriptor;
fn before(self, label: impl SystemLabel) -> ExclusiveSystemDescriptor;
fn after(self, label: impl SystemLabel) -> ExclusiveSystemDescriptor;
fn in_ambiguity_set(self, set: impl AmbiguitySetLabel) -> ExclusiveSystemDescriptor;
fn at_start(self) -> ExclusiveSystemDescriptor;
fn before_commands(self) -> ExclusiveSystemDescriptor;
fn at_end(self) -> ExclusiveSystemDescriptor;
}
impl ExclusiveSystemDescriptorCoercion for ExclusiveSystemDescriptor {
fn with_run_criteria<Marker>(
mut self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> ExclusiveSystemDescriptor {
self.run_criteria = Some(run_criteria.into());
self
}
fn label(mut self, label: impl SystemLabel) -> ExclusiveSystemDescriptor {
self.labels.push(Box::new(label));
self
}
fn before(mut self, label: impl SystemLabel) -> ExclusiveSystemDescriptor {
self.before.push(Box::new(label));
self
}
fn after(mut self, label: impl SystemLabel) -> ExclusiveSystemDescriptor {
self.after.push(Box::new(label));
self
}
fn in_ambiguity_set(mut self, set: impl AmbiguitySetLabel) -> ExclusiveSystemDescriptor {
self.ambiguity_sets.push(Box::new(set));
self
}
fn at_start(mut self) -> ExclusiveSystemDescriptor {
self.insertion_point = InsertionPoint::AtStart;
self
}
fn before_commands(mut self) -> ExclusiveSystemDescriptor {
self.insertion_point = InsertionPoint::BeforeCommands;
self
}
fn at_end(mut self) -> ExclusiveSystemDescriptor {
self.insertion_point = InsertionPoint::AtEnd;
self
}
}
impl<T> ExclusiveSystemDescriptorCoercion for T
where
T: ExclusiveSystem + 'static,
{
fn with_run_criteria<Marker>(
self,
run_criteria: impl IntoRunCriteria<Marker>,
) -> ExclusiveSystemDescriptor {
new_exclusive_descriptor(Box::new(self)).with_run_criteria(run_criteria)
}
fn label(self, label: impl SystemLabel) -> ExclusiveSystemDescriptor {
new_exclusive_descriptor(Box::new(self)).label(label)
}
fn before(self, label: impl SystemLabel) -> ExclusiveSystemDescriptor {
new_exclusive_descriptor(Box::new(self)).before(label)
}
fn after(self, label: impl SystemLabel) -> ExclusiveSystemDescriptor {
new_exclusive_descriptor(Box::new(self)).after(label)
}
fn in_ambiguity_set(self, set: impl AmbiguitySetLabel) -> ExclusiveSystemDescriptor {
new_exclusive_descriptor(Box::new(self)).in_ambiguity_set(set)
}
fn at_start(self) -> ExclusiveSystemDescriptor {
new_exclusive_descriptor(Box::new(self)).at_start()
}
fn before_commands(self) -> ExclusiveSystemDescriptor {
new_exclusive_descriptor(Box::new(self)).before_commands()
}
fn at_end(self) -> ExclusiveSystemDescriptor {
new_exclusive_descriptor(Box::new(self)).at_end()
}
}