From 090ac8780fc4ee1f343319b0a8a03e24934d4c57 Mon Sep 17 00:00:00 2001 From: Tony Garnock-Jones Date: Sun, 12 Nov 2023 10:14:54 +0100 Subject: [PATCH] Add "KeepAlive" for when a driver is still getting ready to expose an Entity but hasn't done so yet. --- syndicate/src/actor.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/syndicate/src/actor.rs b/syndicate/src/actor.rs index 19c807f..992ee60 100644 --- a/syndicate/src/actor.rs +++ b/syndicate/src/actor.rs @@ -347,7 +347,7 @@ pub struct ActorRef { /// A combination of an [`ActorRef`] with a [`FacetId`], acting as a capability to enter the /// execution context of a facet from a linked task. -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct FacetRef { pub actor: ActorRef, pub facet_id: FacetId, @@ -1986,6 +1986,9 @@ impl Facet { } } +#[derive(Debug, Clone)] +pub struct KeepAlive(Option>); + impl ActorRef { /// Uses an internal mutex to access the internal state: takes the /// lock, calls `f` with the internal state, releases the lock, @@ -2005,6 +2008,16 @@ impl ActorRef { }) } + /// Creates a [`KeepAlive`] for (usually temporarily) ensuring an [Actor] does not get + /// garbage-collected due to no references to its [Mailbox] being held. (It may of course + /// be terminated for other reasons.) + pub fn keep_alive(&self) -> KeepAlive { + KeepAlive(self.access(|s| match s { + ActorState::Terminated { .. } => None, + ActorState::Running(ra) => Some(ra.mailbox()), + })) + } + fn facet_ref(&self, facet_id: FacetId) -> FacetRef { FacetRef { actor: self.clone(),