diff --git a/src/actions.rs b/src/actions.rs index fe18d1ae..25c3d975 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -155,7 +155,6 @@ impl TriggerLike for LogicalAction { self.0.id } } - /*#[cfg(test)] //fixme mod test { use ActionPresence::{NotPresent, Present}; diff --git a/src/scheduler/context.rs b/src/scheduler/context.rs index 53e06027..c8e007d5 100644 --- a/src/scheduler/context.rs +++ b/src/scheduler/context.rs @@ -304,7 +304,9 @@ where /// Schedule an action to trigger at some point in the future. /// The action will trigger after its own implicit time delay, - /// plus an optional additional time delay (see [Offset]). + /// plus an optional additional time delay (see [Offset]). This + /// delay is added to the current logical (resp. physical) time + /// for logical (resp. physical) actions. /// /// This is like [Self::schedule_with_v], where the value is [None]. /// @@ -320,7 +322,7 @@ where /// ctx.schedule(action, After(Duration::from_millis(2))); // equivalent to the previous /// ``` #[inline] - pub fn schedule(&mut self, action: &mut LogicalAction, offset: Offset) { + pub fn schedule(&mut self, action: &mut impl SchedulableAsAction, offset: Offset) { self.schedule_with_v(action, None, offset) } @@ -334,7 +336,9 @@ where /// (see [Self::is_present]). /// /// The action will trigger after its own implicit time delay, - /// plus an optional additional time delay (see [Offset]). + /// plus an optional additional time delay (see [Offset]). This + /// delay is added to the current logical (resp. physical) time + /// for logical (resp. physical) actions. /// /// ### Examples /// @@ -350,11 +354,8 @@ where /// ctx.schedule(action, Asap); /// ``` #[inline] - pub fn schedule_with_v(&mut self, action: &mut LogicalAction, value: Option, offset: Offset) { - let eta = self.make_successor_tag(action.0.min_delay + offset.to_duration()); - action.0.schedule_future_value(eta, value); - let downstream = self.dataflow.reactions_triggered_by(&action.get_id()); - self.enqueue_later(downstream, eta); + pub fn schedule_with_v(&mut self, action: &mut impl SchedulableAsAction, value: Option, offset: Offset) { + action.schedule_with_v(self, value, offset) } /// Add new reactions to execute later (at least 1 microstep later). @@ -685,6 +686,34 @@ impl AsyncCtx<'_, '_, '_> { } } +/// Implemented by LogicalAction and PhysicalAction references +/// to give access to [ReactionCtx::schedule] and variants. +pub trait SchedulableAsAction { + #[doc(hidden)] + fn schedule_with_v(&mut self, ctx: &mut ReactionCtx, value: Option, offset: Offset); +} + +impl SchedulableAsAction for LogicalAction { + fn schedule_with_v(&mut self, ctx: &mut ReactionCtx, value: Option, offset: Offset) { + let eta = ctx.make_successor_tag(self.0.min_delay + offset.to_duration()); + self.0.schedule_future_value(eta, value); + let downstream = ctx.dataflow.reactions_triggered_by(&self.get_id()); + ctx.enqueue_later(downstream, eta); + } +} + +impl SchedulableAsAction for PhysicalActionRef { + fn schedule_with_v(&mut self, ctx: &mut ReactionCtx, value: Option, offset: Offset) { + self.use_mut_p(value, |action, value| { + let tag = EventTag::absolute(ctx.initial_time, Instant::now() + offset.to_duration()); + action.0.schedule_future_value(tag, value); + let downstream = ctx.dataflow.reactions_triggered_by(&action.get_id()); + ctx.enqueue_later(downstream, tag); + }) + .ok(); + } +} + /// An offset from the current event. /// /// This is to be used with [ReactionCtx::schedule]. diff --git a/src/test/stuff_that_must_compile.rs b/src/test/stuff_that_must_compile.rs index ccc408f0..090029fa 100644 --- a/src/test/stuff_that_must_compile.rs +++ b/src/test/stuff_that_must_compile.rs @@ -41,6 +41,13 @@ fn actions_get(ctx: &mut ReactionCtx, act_mut: &mut LogicalAction, act: &Lo assert!(ctx.get(act).is_some()); } +fn actions_schedule(ctx: &mut ReactionCtx, logical: &mut LogicalAction, physical: &mut PhysicalActionRef) { + ctx.schedule_with_v(physical, Some(2), Asap); + ctx.schedule(physical, Asap); + ctx.schedule_with_v(logical, Some(2), Asap); + ctx.schedule(logical, Asap); +} + fn actions_use_ref_mut(ctx: &mut ReactionCtx, act: &mut LogicalAction) { // the duplication is useful here, we're testing that `act` is // not moved in the first statement, which would make the