Skip to content

Commit c8d42e7

Browse files
committed
register actor with state
1 parent ba5f702 commit c8d42e7

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
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.4"
3+
version = "0.14.1"
44
edition = "2021"
55
repository = "https://github.com/EnvOut/uactor"
66
license = "MIT"

src/uactor/src/actor/abstract_actor.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::actor::context::ActorContext;
22
use crate::actor::message::Message;
33
use std::future::Future;
4-
use std::sync::Arc;
54
use crate::actor::select::{ActorSelect, SelectError, SelectResult};
65

76
pub trait State: std::any::Any + Send + 'static {}
@@ -18,11 +17,7 @@ pub trait Actor: Sized + Unpin + 'static {
1817

1918
type Inject: Inject + Sized;
2019

21-
type State: Default + Send + Sync + Clone;
22-
23-
fn create_state(&mut self) -> Arc<Self::State> {
24-
Arc::new(Default::default())
25-
}
20+
type State: Send + Sync + Clone;
2621

2722
fn on_start(
2823
&mut self,

src/uactor/src/system/global.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,27 @@ impl GlobalSystem {
2525
) -> (R, tokio::sync::mpsc::UnboundedReceiver<M>)
2626
where
2727
A: Actor,
28+
A::State: Default,
2829
M: Message + Send + 'static,
2930
R: From<(ActorName, UnboundedSender<M>, A::State)>,
3031
{
31-
let mut system = GLOBAL_SYSTEM.write().await;
32-
system.register_ref::<A, M , R>(actor_name).await
32+
let mut system = GLOBAL_SYSTEM.write().await;
33+
system.register_ref::<A, M, R>(actor_name).await
34+
}
35+
36+
pub async fn register_ref_with_state<A, M, R>(
37+
&mut self,
38+
actor_name: &str,
39+
state: A::State,
40+
) -> (R, tokio::sync::mpsc::UnboundedReceiver<M>)
41+
where
42+
A: Actor,
43+
A::State: Default,
44+
M: Message + Send + 'static,
45+
R: From<(ActorName, UnboundedSender<M>, A::State)>,
46+
{
47+
let mut system = GLOBAL_SYSTEM.write().await;
48+
system.register_ref_with_state::<A, M, R>(actor_name, state).await
3349
}
3450

3551
pub async fn spawn_actor<A, S>(

src/uactor/src/system/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ impl System {
4040
self
4141
}
4242

43-
pub async fn register_ref<A, M, R>(
43+
pub async fn register_ref_with_state<A, M, R>(
4444
&mut self,
4545
actor_name: &str,
46+
state: A::State,
4647
) -> (R, tokio::sync::mpsc::UnboundedReceiver<M>)
4748
where
4849
A: Actor,
@@ -52,7 +53,6 @@ impl System {
5253
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<M>();
5354

5455
let actor_name: Arc<str> = actor_name.to_owned().into();
55-
let state = A::State::default();
5656
let old_ref = self.actor_registry.register_ref::<A, M, UnboundedSender<M>>(actor_name.clone(), tx.clone(), state.clone());
5757
if let Some(_old_ref) = old_ref {
5858
tracing::warn!("The actor: {actor_name:?} has already been registered, old ref has been replaced");
@@ -61,6 +61,20 @@ impl System {
6161
(R::from((actor_name, tx, state)), rx)
6262
}
6363

64+
pub async fn register_ref<A, M, R>(
65+
&mut self,
66+
actor_name: &str,
67+
) -> (R, tokio::sync::mpsc::UnboundedReceiver<M>)
68+
where
69+
A: Actor,
70+
A::State: Default,
71+
M: Message + Send + 'static,
72+
R: From<(ActorName, UnboundedSender<M>, A::State)>,
73+
{
74+
let state = A::State::default();
75+
self.register_ref_with_state::<A, M, R>(actor_name, state).await
76+
}
77+
6478
pub async fn spawn_actor<A, S>(
6579
&mut self,
6680
actor_name: Arc<str>,

0 commit comments

Comments
 (0)