Skip to content

Commit 9422748

Browse files
committed
extended Actor trait with on_die method
1 parent 27ffe45 commit 9422748

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uactor/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "uactor"
3-
version = "0.13.0"
3+
version = "0.13.1"
44
edition = "2021"
55
repository = "https://github.com/EnvOut/uactor"
66
license = "MIT"

src/uactor/src/actor/abstract_actor.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::actor::context::ActorContext;
1+
use crate::actor::context::{ActorContext, ContextResult};
22
use crate::actor::message::Message;
33
use std::future::Future;
44
use std::pin::Pin;
@@ -11,6 +11,7 @@ pub trait State: std::any::Any + Send + 'static {}
1111
impl<T: std::any::Any + Send + 'static> State for T {}
1212

1313
use crate::dependency_injection::Inject;
14+
use crate::system::System;
1415

1516
#[allow(unused_variables)]
1617
pub trait Actor: Sized + Unpin + 'static {
@@ -62,6 +63,11 @@ pub trait Actor: Sized + Unpin + 'static {
6263
tracing::error!("Actor error: {:?}", error);
6364
}
6465
}
66+
67+
#[inline]
68+
fn on_die(mut self, ctx: &mut Self::Context, state: &Self::State) -> impl Future<Output = ()> + Send {
69+
async {}
70+
}
6571
}
6672

6773
pub trait Handler<M>

src/uactor/src/system/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ impl System {
103103
}
104104
}
105105

106-
107106
actor.on_start(&mut inject, &mut ctx).await;
108107

109108
// main loop
@@ -133,15 +132,20 @@ impl System {
133132
}
134133
}
135134
}
136-
// call on_die
135+
136+
// destroy context
137137
match ctx.on_die(actor_name.clone()) {
138138
Ok(_) => {
139-
tracing::trace!("The actor: {actor_name:?} is dead");
139+
tracing::trace!("Context of the actor: {actor_name:?} destroyed");
140140
}
141141
Err(err) => {
142-
tracing::error!("Error during actor die: {err:?}");
142+
tracing::error!("Error during context die of the actor. {actor_name:?}: {err:?}");
143143
}
144144
}
145+
146+
// destroy actor as object
147+
148+
actor.on_die(&mut ctx, &state).await
145149
})
146150
};
147151

0 commit comments

Comments
 (0)