From 5a8a508fdca9fde416eff8bde92af409b3816afb Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Fri, 24 Sep 2021 16:14:55 +0200 Subject: [PATCH] More general on_stop; the old behaviour is now at on_stop_notify --- syndicate/src/actor.rs | 21 ++++++++++++++++++--- syndicate/src/during.rs | 2 +- syndicate/src/supervise.rs | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/syndicate/src/actor.rs b/syndicate/src/actor.rs index 153eb77..44097fb 100644 --- a/syndicate/src/actor.rs +++ b/syndicate/src/actor.rs @@ -185,7 +185,7 @@ pub trait Entity: Send { /// Optional callback for running actions when the entity's owning [Facet] terminates /// cleanly. Will not be called in case of abnormal shutdown (crash) of an actor. /// - /// Programs register an entity's stop hook with [Activation::on_stop]. + /// Programs register an entity's stop hook with [Activation::on_stop_notify]. /// /// The default implementation does nothing. fn stop(&mut self, turn: &mut Activation) -> ActorResult { @@ -804,18 +804,27 @@ impl<'activation> Activation<'activation> { /// Registers the entity `r` in the list of stop actions for the active facet. If the facet /// terminates cleanly, `r`'s [`stop`][Entity::stop] will be called. /// - /// **Note.** If the actor crashes, the stop actions will *not* be called. + /// **Note.** If the actor crashes, stop actions will *not* be called. /// /// Use [`RunningActor::add_exit_hook`] to install a callback that will be called at the /// end of the lifetime of the *actor* rather than the facet. (Also, exit hooks are called /// no matter whether actor termination was normal or abnormal.) - pub fn on_stop(&mut self, r: &Arc>) { + pub fn on_stop_notify(&mut self, r: &Arc>) { if let Some(f) = self.active_facet() { let r = Arc::clone(r); f.stop_actions.push(Box::new(move |t| r.internal_with_entity(|e| e.stop(t)))); } } + /// Registers `action` in the list of stop actions for the active facet. If the facet + /// terminates cleanly, `action` will be called. See also notes against + /// [`on_stop_notify`][Activation::on_stop_notify]. + pub fn on_stop ActorResult>(&mut self, action: F) { + if let Some(f) = self.active_facet() { + f.stop_actions.push(Box::new(action)); + } + } + /// Retrieve the [`Account`] against which actions are recorded. pub fn account(&self) -> &Arc { &self.pending.account @@ -1921,6 +1930,12 @@ impl Entity for StopOnRetract { } } +impl ActorResult> Entity for F { + fn message(&mut self, t: &mut Activation, _m: Synced) -> ActorResult { + self(t) + } +} + /// A convenient Syndicate-enhanced variation on /// [`tracing::info_span`]. /// diff --git a/syndicate/src/during.rs b/syndicate/src/during.rs index 410e4de..048be81 100644 --- a/syndicate/src/during.rs +++ b/syndicate/src/during.rs @@ -175,7 +175,7 @@ where let should_register_exit_hook = self.exit_handler.is_some(); let r = t.create(self); if should_register_stop_action { - t.on_stop(&r); + t.on_stop_notify(&r); } if should_register_exit_hook { t.state.add_exit_hook(&r); diff --git a/syndicate/src/supervise.rs b/syndicate/src/supervise.rs index bec2f9b..815dea9 100644 --- a/syndicate/src/supervise.rs +++ b/syndicate/src/supervise.rs @@ -165,7 +165,7 @@ impl ActorResult> Supervisor