Add "KeepAlive" for when a driver is still getting ready to expose an Entity but hasn't done so yet.

This commit is contained in:
Tony Garnock-Jones 2023-11-12 10:14:54 +01:00
parent bbaacd3038
commit 090ac8780f
1 changed files with 14 additions and 1 deletions

View File

@ -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<Arc<Mailbox>>);
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(),