Skip to content

Commit 0a6179d

Browse files
authored
AF-4032: Refactor (#4)
1 parent 9d3934a commit 0a6179d

File tree

13 files changed

+353
-213
lines changed

13 files changed

+353
-213
lines changed

src/uactor/examples/base_sample.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use std::time::Duration;
22

33
use tracing::level_filters::LevelFilter;
4-
use tracing_subscriber::fmt::writer::MakeWriterExt;
54
use tracing_subscriber::layer::SubscriberExt;
65
use tracing_subscriber::util::SubscriberInitExt;
76

8-
use uactor::select::ActorSelect;
97
use uactor::system::System;
108

119
use crate::actor1::Actor1;
@@ -37,7 +35,7 @@ pub mod messages {
3735
}
3836

3937
pub mod actor1 {
40-
use uactor::actor::{Actor, Handler, HandleResult};
38+
use uactor::actor::{Actor, HandleResult, Handler};
4139
use uactor::context::Context;
4240

4341
use crate::messages::{PingPongMsg, ReqMsg, RespMsg};
@@ -52,14 +50,24 @@ pub mod actor1 {
5250
}
5351

5452
impl Handler<PingPongMsg> for Actor1 {
55-
async fn handle(&mut self, _: &mut Self::Inject, msg: PingPongMsg, ctx: &mut Self::Context) -> HandleResult {
53+
async fn handle(
54+
&mut self,
55+
_: &mut Self::Inject,
56+
msg: PingPongMsg,
57+
_ctx: &mut Self::Context,
58+
) -> HandleResult {
5659
println!("actor1 handle PingPongMsg: {msg:?}");
5760
Ok(())
5861
}
5962
}
6063

6164
impl Handler<ReqMsg> for Actor1 {
62-
async fn handle(&mut self, _: &mut Self::Inject, msg: ReqMsg, ctx: &mut Self::Context) -> HandleResult {
65+
async fn handle(
66+
&mut self,
67+
_: &mut Self::Inject,
68+
msg: ReqMsg,
69+
_ctx: &mut Self::Context,
70+
) -> HandleResult {
6371
println!("actor1 handle ReqMsg: {msg:?}");
6472
self.resp_tx.send(RespMsg::Ok).await?;
6573
Ok(())
@@ -68,15 +76,20 @@ pub mod actor1 {
6876
}
6977

7078
pub mod actor2 {
71-
use uactor::actor::{Actor, Handler, HandleResult};
79+
use uactor::actor::{Actor, HandleResult, Handler};
7280
use uactor::context::Context;
7381

7482
use crate::messages::RespMsg;
7583

7684
pub struct Actor2;
7785

7886
impl Handler<RespMsg> for Actor2 {
79-
async fn handle(&mut self, _: &mut Self::Inject, msg: RespMsg, _: &mut Self::Context) -> HandleResult {
87+
async fn handle(
88+
&mut self,
89+
_: &mut Self::Inject,
90+
msg: RespMsg,
91+
_: &mut Self::Context,
92+
) -> HandleResult {
8093
println!("actor2 handle RespMsg: {msg:?}");
8194
Ok(())
8295
}

src/uactor/examples/dependency_injection.rs

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use time::ext::NumericalStdDuration;
2-
use tracing_subscriber::layer::SubscriberExt;
3-
use tracing_subscriber::util::SubscriberInitExt;
42
use uactor::actor::MessageSender;
53

64
use uactor::system::System;
@@ -36,9 +34,9 @@ mod messages {
3634
mod actor1 {
3735
use tokio::sync::mpsc::UnboundedSender;
3836

39-
use uactor::actor::{Actor, EmptyState, Handler, HandleResult, MessageSender};
40-
use uactor::context::Context;
37+
use uactor::actor::{Actor, EmptyState, HandleResult, Handler, MessageSender};
4138
use uactor::context::extensions::Service;
39+
use uactor::context::Context;
4240
use uactor::di::{Inject, InjectError};
4341
use uactor::system::System;
4442

@@ -56,11 +54,12 @@ mod actor1 {
5654

5755
impl Inject for Services {
5856
async fn inject(system: &System) -> Result<Self, InjectError>
59-
where
60-
Self: Sized,
57+
where
58+
Self: Sized,
6159
{
6260
let service1 = system.get_service()?;
63-
let actor2_ref = system.get_actor::<Actor2Ref<UnboundedSender<Actor2Msg>>>("actor2".into())?;
61+
let actor2_ref =
62+
system.get_actor::<Actor2Ref<UnboundedSender<Actor2Msg>>>("actor2".into())?;
6463
Ok(Services::new(service1, actor2_ref))
6564
}
6665
}
@@ -71,7 +70,12 @@ mod actor1 {
7170
}
7271

7372
impl Handler<PingMsg> for Actor1 {
74-
async fn handle(&mut self, Services { service1, .. }: &mut Self::Inject, ping: PingMsg, ctx: &mut Context) -> HandleResult {
73+
async fn handle(
74+
&mut self,
75+
Services { service1, .. }: &mut Self::Inject,
76+
ping: PingMsg,
77+
_ctx: &mut Context,
78+
) -> HandleResult {
7579
println!("actor1: Received ping message");
7680

7781
service1.do_something();
@@ -83,21 +87,25 @@ mod actor1 {
8387
}
8488

8589
impl Handler<MessageWithoutReply> for Actor1 {
86-
async fn handle(&mut self, Services { actor2_ref, .. }: &mut Self::Inject, msg: MessageWithoutReply, ctx: &mut Context) -> HandleResult {
90+
async fn handle(
91+
&mut self,
92+
Services { actor2_ref, .. }: &mut Self::Inject,
93+
msg: MessageWithoutReply,
94+
_ctx: &mut Context,
95+
) -> HandleResult {
8796
println!("actor1: Received {msg:?} message, sending PrintMessage to the actor2");
8897
actor2_ref.send(PrintMessage::new(msg.into()))?;
8998
Ok(())
9099
}
91100
}
92101

93102
uactor::generate_actor_ref!(Actor1, { PingMsg, MessageWithoutReply }, EmptyState);
94-
95103
}
96104

97105
mod actor2 {
98-
use uactor::actor::{Actor, EmptyState, Handler, HandleResult};
99-
use uactor::context::Context;
106+
use uactor::actor::{Actor, EmptyState, HandleResult, Handler};
100107
use uactor::context::extensions::Service;
108+
use uactor::context::Context;
101109
use uactor::di::{Inject, InjectError};
102110
use uactor::system::System;
103111

@@ -111,8 +119,8 @@ mod actor2 {
111119

112120
impl Inject for Services {
113121
async fn inject(system: &System) -> Result<Self, InjectError>
114-
where
115-
Self: Sized,
122+
where
123+
Self: Sized,
116124
{
117125
let service2 = system.get_service::<Service2>()?;
118126
Ok(Services(service2))
@@ -125,7 +133,12 @@ mod actor2 {
125133
}
126134

127135
impl Handler<PingMsg> for Actor2 {
128-
async fn handle(&mut self, Services(service2): &mut Self::Inject, ping: PingMsg, _: &mut Context) -> HandleResult {
136+
async fn handle(
137+
&mut self,
138+
Services(service2): &mut Self::Inject,
139+
ping: PingMsg,
140+
_ctx: &mut Context,
141+
) -> HandleResult {
129142
println!("actor2: Received ping message");
130143

131144
service2.do_something();
@@ -137,7 +150,12 @@ mod actor2 {
137150
}
138151

139152
impl Handler<PrintMessage> for Actor2 {
140-
async fn handle(&mut self, _: &mut Self::Inject, msg: PrintMessage, _: &mut Context) -> HandleResult {
153+
async fn handle(
154+
&mut self,
155+
_: &mut Self::Inject,
156+
msg: PrintMessage,
157+
_ctx: &mut Context,
158+
) -> HandleResult {
141159
println!("actor2: Received message: {msg:?}");
142160
Ok(())
143161
}
@@ -192,25 +210,21 @@ async fn main() -> anyhow::Result<()> {
192210
let actor2 = Actor2;
193211
let (actor2_ref, _) = uactor::spawn_with_ref!(system, actor2: Actor2);
194212

195-
196213
// Run actors
197214
system.run_actor::<Actor1>(actor1_ref.name()).await?;
198215
system.run_actor::<Actor2>(actor2_ref.name()).await?;
199216

200217
// Case #1: send messages and call injected (not from &self) services inside handlers
201-
println!("-- Case #1: send messages and call injected (not from &self) services inside handlers");
202-
let pong1 = actor1_ref
203-
.ask::<PongMsg>(|reply| PingMsg(reply))
204-
.await?;
205-
let pong2 = actor2_ref
206-
.ask::<PongMsg>(|reply| PingMsg(reply))
207-
.await?;
218+
println!(
219+
"-- Case #1: send messages and call injected (not from &self) services inside handlers"
220+
);
221+
let pong1 = actor1_ref.ask::<PongMsg>(PingMsg).await?;
222+
let pong2 = actor2_ref.ask::<PongMsg>(PingMsg).await?;
208223
println!("main: received {pong1:?} and {pong2:?} messages");
209224

210225
// Case #2: send message#1 to actor1 and reply to actor2 without actor2 reference inside message#1
211226
println!("\n-- Case #2: send message#1 to actor1 and reply to actor2 without actor2 reference inside message#1");
212-
let pong1 = actor1_ref
213-
.send(MessageWithoutReply("login:password".to_owned()))?;
227+
actor1_ref.send(MessageWithoutReply("login:password".to_owned()))?;
214228

215229
tokio::time::sleep(1.std_milliseconds()).await;
216230
Ok(())

src/uactor/examples/interval.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use anyhow::Context;
21
use time::ext::NumericalStdDuration;
32
use uactor::actor::MessageSender;
43

@@ -23,7 +22,7 @@ mod messages {
2322
}
2423

2524
mod actor1 {
26-
use uactor::actor::{Actor, EmptyState, Handler, HandleResult};
25+
use uactor::actor::{Actor, EmptyState, HandleResult, Handler};
2726
use uactor::context::Context;
2827
use uactor::message::IntervalMessage;
2928

@@ -40,7 +39,12 @@ mod actor1 {
4039
}
4140

4241
impl Handler<PingMsg> for Actor1 {
43-
async fn handle(&mut self, _: &mut Self::Inject, ping: PingMsg, _: &mut Context) -> HandleResult {
42+
async fn handle(
43+
&mut self,
44+
_: &mut Self::Inject,
45+
ping: PingMsg,
46+
_ctx: &mut Context,
47+
) -> HandleResult {
4448
println!("actor1: Received ping message");
4549
let PingMsg(reply) = ping;
4650
let _ = reply.send(PongMsg);
@@ -49,9 +53,20 @@ mod actor1 {
4953
}
5054

5155
impl Handler<IntervalMessage> for Actor1 {
52-
async fn handle(&mut self, _: &mut Self::Inject, IntervalMessage { time: _, duration }: IntervalMessage, _: &mut Context) -> HandleResult {
56+
async fn handle(
57+
&mut self,
58+
_: &mut Self::Inject,
59+
IntervalMessage {
60+
time: _,
61+
duration: _,
62+
}: IntervalMessage,
63+
_ctx: &mut Context,
64+
) -> HandleResult {
5365
self.interval_count += 1;
54-
println!("actor1: received {}nd interval message", self.interval_count);
66+
println!(
67+
"actor1: received {}nd interval message",
68+
self.interval_count
69+
);
5570
Ok(())
5671
}
5772
}
@@ -68,13 +83,11 @@ async fn main() -> anyhow::Result<()> {
6883
// 1 second interval
6984
let interval = tokio::time::interval(1.std_seconds());
7085

71-
let (mut actor1_ref, _) = uactor::spawn_with_ref!(system, actor1: Actor1, interval);
86+
let (actor1_ref, _) = uactor::spawn_with_ref!(system, actor1: Actor1, interval);
7287

7388
system.run_actor::<Actor1>(actor1_ref.name()).await?;
7489

75-
let pong = actor1_ref
76-
.ask::<PongMsg>(|reply| PingMsg(reply))
77-
.await?;
90+
let pong = actor1_ref.ask::<PongMsg>(PingMsg).await?;
7891
println!("main: received {pong:?} message");
7992

8093
// waiting 10 seconds and expecting new message each 1 second

src/uactor/examples/single_channel_actor.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod messages {
2020
}
2121

2222
mod actor1 {
23-
use uactor::actor::{Actor, EmptyState, Handler, HandleResult};
23+
use uactor::actor::{Actor, EmptyState, HandleResult, Handler};
2424
use uactor::context::Context;
2525

2626
use crate::messages::{PingMsg, PongMsg};
@@ -33,7 +33,12 @@ mod actor1 {
3333
}
3434

3535
impl Handler<PingMsg> for Actor1 {
36-
async fn handle(&mut self, _: &mut Self::Inject, ping: PingMsg, _: &mut Context) -> HandleResult {
36+
async fn handle(
37+
&mut self,
38+
_: &mut Self::Inject,
39+
ping: PingMsg,
40+
_: &mut Context,
41+
) -> HandleResult {
3742
println!("actor1: Received ping message");
3843
let PingMsg(reply) = ping;
3944
let _ = reply.send(PongMsg);
@@ -54,8 +59,7 @@ async fn main() -> anyhow::Result<()> {
5459

5560
system.run_actor::<Actor1>(actor1_ref.name()).await?;
5661

57-
let pong = actor1_ref.ask(|reply| PingMsg(reply))
58-
.await?;
62+
let pong = actor1_ref.ask(PingMsg).await?;
5963
println!("main: received {pong:?} message");
6064

6165
Ok(())

src/uactor/examples/supervised_actor.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::ops::Shl;
21
use std::time::Duration;
32
use tracing::level_filters::LevelFilter;
43
use tracing_subscriber::layer::SubscriberExt;
@@ -26,12 +25,12 @@ mod messages {
2625
}
2726

2827
mod actor1 {
29-
use tokio::sync::mpsc;
30-
use uactor::actor::{Actor, EmptyState, Handler, HandleResult, MessageSender};
31-
use uactor::context::ActorContext;
32-
use uactor::context::supervised::SupervisedContext;
3328
use crate::messages::{PingMsg, PongMsg};
3429
use crate::supervisor::{SupervisorMsg, SupervisorRef};
30+
use tokio::sync::mpsc;
31+
use uactor::actor::{Actor, EmptyState, HandleResult, Handler};
32+
use uactor::context::supervised::SupervisedContext;
33+
use uactor::context::ActorContext;
3534

3635
pub struct Actor1;
3736

@@ -41,7 +40,12 @@ mod actor1 {
4140
}
4241

4342
impl Handler<PingMsg> for Actor1 {
44-
async fn handle(&mut self, _: &mut Self::Inject, ping: PingMsg, ctx: &mut Self::Context) -> HandleResult {
43+
async fn handle(
44+
&mut self,
45+
_: &mut Self::Inject,
46+
ping: PingMsg,
47+
ctx: &mut Self::Context,
48+
) -> HandleResult {
4549
println!("actor1: Received ping message");
4650
let PingMsg(reply) = ping;
4751
let _ = reply.send(PongMsg);
@@ -54,9 +58,8 @@ mod actor1 {
5458
}
5559

5660
mod supervisor {
57-
use uactor::actor::{Actor, EmptyState, Handler, HandleResult, MessageSender};
61+
use uactor::actor::{Actor, EmptyState, HandleResult, Handler};
5862
use uactor::context::{ActorDied, Context};
59-
use uactor::data_publisher::{DataPublisher, DataPublisherResult, TryClone};
6063

6164
pub struct Supervisor;
6265

@@ -66,7 +69,12 @@ mod supervisor {
6669
}
6770

6871
impl Handler<ActorDied> for Supervisor {
69-
async fn handle(&mut self, _: &mut Self::Inject, ActorDied(name): ActorDied, _: &mut Context) -> HandleResult {
72+
async fn handle(
73+
&mut self,
74+
_: &mut Self::Inject,
75+
ActorDied(name): ActorDied,
76+
_: &mut Context,
77+
) -> HandleResult {
7078
println!("Actor with name: {name:?} - died");
7179
Ok(())
7280
}
@@ -90,11 +98,12 @@ async fn main() -> anyhow::Result<()> {
9098
let (actor1_ref, _) = uactor::spawn_with_ref!(system, actor1: Actor1);
9199
let (supervisor_ref, _) = uactor::spawn_with_ref!(system, supervisor: Supervisor);
92100

93-
system.run_actor::<Supervisor>(supervisor_ref.name()).await?;
101+
system
102+
.run_actor::<Supervisor>(supervisor_ref.name())
103+
.await?;
94104
system.run_actor::<Actor1>(actor1_ref.name()).await?;
95105

96-
let pong = actor1_ref.ask(|reply| PingMsg(reply))
97-
.await?;
106+
let pong = actor1_ref.ask(PingMsg).await?;
98107
println!("main: received {pong:?} message");
99108

100109
tokio::time::sleep(Duration::from_secs(1)).await;

0 commit comments

Comments
 (0)