Skip to content

Commit 15356fc

Browse files
committed
Different types for Cast and Call messages
1 parent 31656cc commit 15356fc

File tree

10 files changed

+52
-44
lines changed

10 files changed

+52
-44
lines changed

concurrency/src/tasks/gen_server.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl<G: GenServer> GenServerHandle<G> {
4242
self.tx.clone()
4343
}
4444

45-
pub async fn call(&mut self, message: G::InMsg) -> Result<G::OutMsg, GenServerError> {
45+
pub async fn call(&mut self, message: G::CallMsg) -> Result<G::OutMsg, GenServerError> {
4646
let (oneshot_tx, oneshot_rx) = oneshot::channel::<Result<G::OutMsg, GenServerError>>();
4747
self.tx.send(GenServerInMsg::Call {
4848
sender: oneshot_tx,
@@ -54,7 +54,7 @@ impl<G: GenServer> GenServerHandle<G> {
5454
}
5555
}
5656

57-
pub async fn cast(&mut self, message: G::InMsg) -> Result<(), GenServerError> {
57+
pub async fn cast(&mut self, message: G::CastMsg) -> Result<(), GenServerError> {
5858
tracing::info!("Sending");
5959
self.tx
6060
.send(GenServerInMsg::Cast { message })
@@ -65,10 +65,10 @@ impl<G: GenServer> GenServerHandle<G> {
6565
pub enum GenServerInMsg<A: GenServer> {
6666
Call {
6767
sender: oneshot::Sender<Result<A::OutMsg, GenServerError>>,
68-
message: A::InMsg,
68+
message: A::CallMsg,
6969
},
7070
Cast {
71-
message: A::InMsg,
71+
message: A::CastMsg,
7272
},
7373
}
7474

@@ -86,7 +86,8 @@ pub trait GenServer
8686
where
8787
Self: Send + Sized,
8888
{
89-
type InMsg: Send + Sized;
89+
type CallMsg: Send + Sized;
90+
type CastMsg: Send + Sized;
9091
type OutMsg: Send + Sized;
9192
type State: Clone + Send;
9293
type Error: Debug;
@@ -188,14 +189,14 @@ where
188189

189190
fn handle_call(
190191
&mut self,
191-
message: Self::InMsg,
192+
message: Self::CallMsg,
192193
handle: &GenServerHandle<Self>,
193194
state: &mut Self::State,
194195
) -> impl std::future::Future<Output = CallResponse<Self::OutMsg>> + Send;
195196

196197
fn handle_cast(
197198
&mut self,
198-
message: Self::InMsg,
199+
message: Self::CastMsg,
199200
handle: &GenServerHandle<Self>,
200201
state: &mut Self::State,
201202
) -> impl std::future::Future<Output = CastResponse> + Send;

concurrency/src/tasks/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{GenServer, GenServerHandle};
99
pub fn send_after<T>(
1010
period: Duration,
1111
mut handle: GenServerHandle<T>,
12-
message: T::InMsg,
12+
message: T::CastMsg,
1313
) -> JoinHandle<()>
1414
where
1515
T: GenServer + 'static,

concurrency/src/threads/gen_server.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl<G: GenServer> GenServerHandle<G> {
4343
self.tx.clone()
4444
}
4545

46-
pub fn call(&mut self, message: G::InMsg) -> Result<G::OutMsg, GenServerError> {
46+
pub fn call(&mut self, message: G::CallMsg) -> Result<G::OutMsg, GenServerError> {
4747
let (oneshot_tx, oneshot_rx) = oneshot::channel::<Result<G::OutMsg, GenServerError>>();
4848
self.tx.send(GenServerInMsg::Call {
4949
sender: oneshot_tx,
@@ -55,7 +55,7 @@ impl<G: GenServer> GenServerHandle<G> {
5555
}
5656
}
5757

58-
pub fn cast(&mut self, message: G::InMsg) -> Result<(), GenServerError> {
58+
pub fn cast(&mut self, message: G::CastMsg) -> Result<(), GenServerError> {
5959
self.tx
6060
.send(GenServerInMsg::Cast { message })
6161
.map_err(|_error| GenServerError::ServerError)
@@ -65,10 +65,10 @@ impl<G: GenServer> GenServerHandle<G> {
6565
pub enum GenServerInMsg<A: GenServer> {
6666
Call {
6767
sender: oneshot::Sender<Result<A::OutMsg, GenServerError>>,
68-
message: A::InMsg,
68+
message: A::CallMsg,
6969
},
7070
Cast {
71-
message: A::InMsg,
71+
message: A::CastMsg,
7272
},
7373
}
7474

@@ -86,7 +86,8 @@ pub trait GenServer
8686
where
8787
Self: Send + Sized,
8888
{
89-
type InMsg: Send + Sized;
89+
type CallMsg: Send + Sized;
90+
type CastMsg: Send + Sized;
9091
type OutMsg: Send + Sized;
9192
type State: Clone + Send;
9293
type Error: Debug;
@@ -176,14 +177,14 @@ where
176177

177178
fn handle_call(
178179
&mut self,
179-
message: Self::InMsg,
180+
message: Self::CallMsg,
180181
handle: &GenServerHandle<Self>,
181182
state: &mut Self::State,
182183
) -> CallResponse<Self::OutMsg>;
183184

184185
fn handle_cast(
185186
&mut self,
186-
message: Self::InMsg,
187+
message: Self::CastMsg,
187188
handle: &GenServerHandle<Self>,
188189
state: &mut Self::State,
189190
) -> CastResponse;

concurrency/src/threads/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{GenServer, GenServerHandle};
99
pub fn send_after<T>(
1010
period: Duration,
1111
mut handle: GenServerHandle<T>,
12-
message: T::InMsg,
12+
message: T::CastMsg,
1313
) -> JoinHandle<()>
1414
where
1515
T: GenServer + 'static,

examples/bank/src/server.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ impl Bank {
4141
}
4242

4343
impl GenServer for Bank {
44-
type InMsg = InMessage;
44+
type CallMsg = InMessage;
45+
type CastMsg = ();
4546
type OutMsg = MsgResult;
4647
type Error = BankError;
4748
type State = BankState;
@@ -52,19 +53,19 @@ impl GenServer for Bank {
5253

5354
async fn handle_call(
5455
&mut self,
55-
message: InMessage,
56+
message: Self::CallMsg,
5657
_handle: &BankHandle,
5758
state: &mut Self::State,
5859
) -> CallResponse<Self::OutMsg> {
5960
match message.clone() {
60-
InMessage::New { who } => match state.get(&who) {
61+
Self::CallMsg::New { who } => match state.get(&who) {
6162
Some(_amount) => CallResponse::Reply(Err(BankError::AlreadyACustomer { who })),
6263
None => {
6364
state.insert(who.clone(), 0);
6465
CallResponse::Reply(Ok(OutMessage::Welcome { who }))
6566
}
6667
},
67-
InMessage::Add { who, amount } => match state.get(&who) {
68+
Self::CallMsg::Add { who, amount } => match state.get(&who) {
6869
Some(current) => {
6970
let new_amount = current + amount;
7071
state.insert(who.clone(), new_amount);
@@ -75,7 +76,7 @@ impl GenServer for Bank {
7576
}
7677
None => CallResponse::Reply(Err(BankError::NotACustomer { who })),
7778
},
78-
InMessage::Remove { who, amount } => match state.get(&who) {
79+
Self::CallMsg::Remove { who, amount } => match state.get(&who) {
7980
Some(current) => match current < &amount {
8081
true => CallResponse::Reply(Err(BankError::InsufficientBalance {
8182
who,
@@ -92,13 +93,13 @@ impl GenServer for Bank {
9293
},
9394
None => CallResponse::Reply(Err(BankError::NotACustomer { who })),
9495
},
95-
InMessage::Stop => CallResponse::Stop(Ok(OutMessage::Stopped)),
96+
Self::CallMsg::Stop => CallResponse::Stop(Ok(OutMessage::Stopped)),
9697
}
9798
}
9899

99100
async fn handle_cast(
100101
&mut self,
101-
_message: InMessage,
102+
_message: Self::CastMsg,
102103
_handle: &BankHandle,
103104
_state: &mut Self::State,
104105
) -> CastResponse {

examples/bank_threads/src/server.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ impl Bank {
3737
}
3838

3939
impl GenServer for Bank {
40-
type InMsg = InMessage;
40+
type CallMsg = InMessage;
41+
type CastMsg = ();
4142
type OutMsg = MsgResult;
4243
type Error = BankError;
4344
type State = BankState;
@@ -48,7 +49,7 @@ impl GenServer for Bank {
4849

4950
fn handle_call(
5051
&mut self,
51-
message: InMessage,
52+
message: Self::CallMsg,
5253
_handle: &BankHandle,
5354
state: &mut Self::State,
5455
) -> CallResponse<Self::OutMsg> {
@@ -94,7 +95,7 @@ impl GenServer for Bank {
9495

9596
fn handle_cast(
9697
&mut self,
97-
_message: InMessage,
98+
_message: Self::CastMsg,
9899
_handle: &BankHandle,
99100
_state: &mut Self::State,
100101
) -> CastResponse {

examples/name_server/src/server.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ impl NameServer {
2626
}
2727

2828
impl GenServer for NameServer {
29-
type InMsg = InMessage;
29+
type CallMsg = InMessage;
30+
type CastMsg = ();
3031
type OutMsg = OutMessage;
3132
type Error = std::fmt::Error;
3233
type State = NameServerState;
@@ -37,16 +38,16 @@ impl GenServer for NameServer {
3738

3839
async fn handle_call(
3940
&mut self,
40-
message: InMessage,
41+
message: Self::CallMsg,
4142
_handle: &NameServerHandle,
4243
state: &mut Self::State,
4344
) -> CallResponse<Self::OutMsg> {
4445
match message.clone() {
45-
Self::InMsg::Add { key, value } => {
46+
Self::CallMsg::Add { key, value } => {
4647
state.insert(key, value);
4748
CallResponse::Reply(Self::OutMsg::Ok)
4849
}
49-
Self::InMsg::Find { key } => match state.get(&key) {
50+
Self::CallMsg::Find { key } => match state.get(&key) {
5051
Some(value) => CallResponse::Reply(Self::OutMsg::Found {
5152
value: value.to_string(),
5253
}),
@@ -57,7 +58,7 @@ impl GenServer for NameServer {
5758

5859
async fn handle_cast(
5960
&mut self,
60-
_message: InMessage,
61+
_message: Self::CastMsg,
6162
_handle: &NameServerHandle,
6263
_state: &mut Self::State,
6364
) -> CastResponse {

examples/name_server_with_error/src/server.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ impl NameServer {
2929
}
3030

3131
impl GenServer for NameServer {
32-
type InMsg = InMessage;
32+
type CallMsg = InMessage;
33+
type CastMsg = ();
3334
type OutMsg = OutMessage;
3435
type Error = std::fmt::Error;
3536
type State = NameServerState;
@@ -40,20 +41,20 @@ impl GenServer for NameServer {
4041

4142
async fn handle_call(
4243
&mut self,
43-
message: InMessage,
44+
message: Self::CallMsg,
4445
_handle: &NameServerHandle,
4546
state: &mut Self::State,
4647
) -> CallResponse<Self::OutMsg> {
4748
match message.clone() {
48-
Self::InMsg::Add { key, value } => {
49+
Self::CallMsg::Add { key, value } => {
4950
state.insert(key.clone(), value);
5051
if key == "error" {
5152
panic!("error!")
5253
} else {
5354
CallResponse::Reply(Self::OutMsg::Ok)
5455
}
5556
}
56-
Self::InMsg::Find { key } => match state.get(&key) {
57+
Self::CallMsg::Find { key } => match state.get(&key) {
5758
Some(value) => CallResponse::Reply(Self::OutMsg::Found {
5859
value: value.to_string(),
5960
}),
@@ -64,7 +65,7 @@ impl GenServer for NameServer {
6465

6566
async fn handle_cast(
6667
&mut self,
67-
_message: InMessage,
68+
_message: Self::CastMsg,
6869
_handle: &NameServerHandle,
6970
_state: &mut Self::State,
7071
) -> CastResponse {

examples/updater/src/server.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ impl UpdaterServer {
2525
}
2626

2727
impl GenServer for UpdaterServer {
28-
type InMsg = InMessage;
28+
type CallMsg = ();
29+
type CastMsg = InMessage;
2930
type OutMsg = OutMessage;
3031
type Error = std::fmt::Error;
3132
type State = UpdateServerState;
@@ -36,7 +37,7 @@ impl GenServer for UpdaterServer {
3637

3738
async fn handle_call(
3839
&mut self,
39-
_message: InMessage,
40+
_message: Self::CallMsg,
4041
_handle: &UpdateServerHandle,
4142
_state: &mut Self::State,
4243
) -> CallResponse<Self::OutMsg> {
@@ -45,12 +46,12 @@ impl GenServer for UpdaterServer {
4546

4647
async fn handle_cast(
4748
&mut self,
48-
message: InMessage,
49+
message: Self::CastMsg,
4950
handle: &UpdateServerHandle,
5051
state: &mut Self::State,
5152
) -> CastResponse {
5253
match message {
53-
Self::InMsg::Check => {
54+
Self::CastMsg::Check => {
5455
send_after(state.periodicity, handle.clone(), InMessage::Check);
5556
let url = state.url.clone();
5657
tracing::info!("Fetching: {url}");

examples/updater_threads/src/server.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ impl UpdaterServer {
2626
}
2727

2828
impl GenServer for UpdaterServer {
29-
type InMsg = InMessage;
29+
type CallMsg = ();
30+
type CastMsg = InMessage;
3031
type OutMsg = OutMessage;
3132
type Error = std::fmt::Error;
3233
type State = UpdateServerState;
@@ -37,7 +38,7 @@ impl GenServer for UpdaterServer {
3738

3839
fn handle_call(
3940
&mut self,
40-
_message: InMessage,
41+
_message: Self::CallMsg,
4142
_handle: &UpdateServerHandle,
4243
_state: &mut Self::State,
4344
) -> CallResponse<Self::OutMsg> {
@@ -46,12 +47,12 @@ impl GenServer for UpdaterServer {
4647

4748
fn handle_cast(
4849
&mut self,
49-
message: InMessage,
50+
message: Self::CastMsg,
5051
handle: &UpdateServerHandle,
5152
state: &mut Self::State,
5253
) -> CastResponse {
5354
match message {
54-
Self::InMsg::Check => {
55+
Self::CastMsg::Check => {
5556
send_after(state.periodicity, handle.clone(), InMessage::Check);
5657
let url = state.url.clone();
5758
tracing::info!("Fetching: {url}");

0 commit comments

Comments
 (0)