Skip to content

Commit c6f757c

Browse files
authored
Merge pull request #19 from lambdaclass/default_handlers
Added default implementation for handle_cast and handle_call
2 parents 18d0082 + 1375b10 commit c6f757c

File tree

15 files changed

+117
-118
lines changed

15 files changed

+117
-118
lines changed

concurrency/src/error.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::{error::Error as StdError, fmt::Display};
2+
3+
#[derive(Debug)]
4+
pub enum GenServerError {
5+
Callback,
6+
Initialization,
7+
Server,
8+
CallMsgUnused,
9+
CastMsgUnused,
10+
}
11+
12+
impl<T> From<spawned_rt::threads::mpsc::SendError<T>> for GenServerError {
13+
fn from(_value: spawned_rt::threads::mpsc::SendError<T>) -> Self {
14+
Self::Server
15+
}
16+
}
17+
18+
impl<T> From<spawned_rt::tasks::mpsc::SendError<T>> for GenServerError {
19+
fn from(_value: spawned_rt::tasks::mpsc::SendError<T>) -> Self {
20+
Self::Server
21+
}
22+
}
23+
24+
impl Display for GenServerError {
25+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26+
match self {
27+
Self::Callback => write!(f, "Callback Error"),
28+
Self::Initialization => write!(f, "Initialization Error"),
29+
Self::Server => write!(f, "Server Error"),
30+
Self::CallMsgUnused => write!(f, "Unsupported Call Messages on this GenServer"),
31+
Self::CastMsgUnused => write!(f, "Unsupported Cast Messages on this GenServer"),
32+
}
33+
}
34+
}
35+
36+
impl StdError for GenServerError {}

concurrency/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! spawned concurrency
22
//! Some basic traits and structs to implement concurrent code à-la-Erlang.
3-
3+
pub mod error;
4+
pub mod messages;
45
pub mod tasks;
56
pub mod threads;

concurrency/src/messages.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[derive(Clone)]
2+
pub struct Unused;

concurrency/src/tasks/error.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

concurrency/src/tasks/gen_server.rs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use futures::future::FutureExt as _;
44
use spawned_rt::tasks::{self as rt, mpsc, oneshot};
55
use std::{fmt::Debug, future::Future, panic::AssertUnwindSafe};
66

7-
use super::error::GenServerError;
7+
use crate::error::GenServerError;
88

99
#[derive(Debug)]
1010
pub struct GenServerHandle<G: GenServer + 'static> {
@@ -93,11 +93,13 @@ pub enum GenServerInMsg<G: GenServer> {
9393

9494
pub enum CallResponse<G: GenServer> {
9595
Reply(G::State, G::OutMsg),
96+
Unused,
9697
Stop(G::OutMsg),
9798
}
9899

99100
pub enum CastResponse<G: GenServer> {
100101
NoReply(G::State),
102+
Unused,
101103
Stop,
102104
}
103105

@@ -181,7 +183,7 @@ where
181183
handle: &GenServerHandle<Self>,
182184
rx: &mut mpsc::Receiver<GenServerInMsg<Self>>,
183185
state: Self::State,
184-
) -> impl std::future::Future<Output = Result<(Self::State, bool), GenServerError>> + Send {
186+
) -> impl Future<Output = Result<(Self::State, bool), GenServerError>> + Send {
185187
async move {
186188
let message = rx.recv().await;
187189

@@ -200,17 +202,21 @@ where
200202
(true, new_state, Ok(response))
201203
}
202204
CallResponse::Stop(response) => (false, state_clone, Ok(response)),
205+
CallResponse::Unused => {
206+
tracing::error!("GenServer received unexpected CallMessage");
207+
(false, state_clone, Err(GenServerError::CallMsgUnused))
208+
}
203209
},
204210
Err(error) => {
205-
tracing::trace!(
211+
tracing::error!(
206212
"Error in callback, reverting state - Error: '{error:?}'"
207213
);
208214
(true, state_clone, Err(GenServerError::Callback))
209215
}
210216
};
211217
// Send response back
212218
if sender.send(response).is_err() {
213-
tracing::trace!(
219+
tracing::error!(
214220
"GenServer failed to send response back, client must have died"
215221
)
216222
};
@@ -224,6 +230,10 @@ where
224230
Ok(response) => match response {
225231
CastResponse::NoReply(new_state) => (true, new_state),
226232
CastResponse::Stop => (false, state_clone),
233+
CastResponse::Unused => {
234+
tracing::error!("GenServer received unexpected CastMessage");
235+
(false, state_clone)
236+
}
227237
},
228238
Err(error) => {
229239
tracing::trace!(
@@ -244,17 +254,21 @@ where
244254

245255
fn handle_call(
246256
&mut self,
247-
message: Self::CallMsg,
248-
handle: &GenServerHandle<Self>,
249-
state: Self::State,
250-
) -> impl std::future::Future<Output = CallResponse<Self>> + Send;
257+
_message: Self::CallMsg,
258+
_handle: &GenServerHandle<Self>,
259+
_state: Self::State,
260+
) -> impl Future<Output = CallResponse<Self>> + Send {
261+
async { CallResponse::Unused }
262+
}
251263

252264
fn handle_cast(
253265
&mut self,
254-
message: Self::CastMsg,
255-
handle: &GenServerHandle<Self>,
256-
state: Self::State,
257-
) -> impl std::future::Future<Output = CastResponse<Self>> + Send;
266+
_message: Self::CastMsg,
267+
_handle: &GenServerHandle<Self>,
268+
_state: Self::State,
269+
) -> impl Future<Output = CastResponse<Self>> + Send {
270+
async { CastResponse::Unused }
271+
}
258272
}
259273

260274
#[cfg(test)]

concurrency/src/tasks/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
//! spawned concurrency
22
//! Runtime tasks-based traits and structs to implement concurrent code à-la-Erlang.
33
4-
mod error;
54
mod gen_server;
65
mod process;
76
mod time;
87

98
#[cfg(test)]
109
mod timer_tests;
1110

12-
pub use error::GenServerError;
1311
pub use gen_server::{CallResponse, CastResponse, GenServer, GenServerHandle, GenServerInMsg};
1412
pub use process::{send, Process, ProcessInfo};
1513
pub use time::{send_after, send_interval};

concurrency/src/threads/error.rs

Lines changed: 0 additions & 12 deletions
This file was deleted.

concurrency/src/threads/gen_server.rs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
panic::{catch_unwind, AssertUnwindSafe},
77
};
88

9-
use super::error::GenServerError;
9+
use crate::error::GenServerError;
1010

1111
#[derive(Debug)]
1212
pub struct GenServerHandle<G: GenServer + 'static> {
@@ -71,11 +71,13 @@ pub enum GenServerInMsg<G: GenServer> {
7171

7272
pub enum CallResponse<G: GenServer> {
7373
Reply(G::State, G::OutMsg),
74+
Unused,
7475
Stop(G::OutMsg),
7576
}
7677

7778
pub enum CastResponse<G: GenServer> {
7879
NoReply(G::State),
80+
Unused,
7981
Stop,
8082
}
8183

@@ -169,6 +171,10 @@ where
169171
(true, new_state, Ok(response))
170172
}
171173
CallResponse::Stop(response) => (false, state_clone, Ok(response)),
174+
CallResponse::Unused => {
175+
tracing::error!("GenServer received unexpected CallMessage");
176+
(false, state_clone, Err(GenServerError::CallMsgUnused))
177+
}
172178
},
173179
Err(error) => {
174180
tracing::trace!(
@@ -190,6 +196,10 @@ where
190196
Ok(response) => match response {
191197
CastResponse::NoReply(new_state) => (true, new_state),
192198
CastResponse::Stop => (false, state_clone),
199+
CastResponse::Unused => {
200+
tracing::error!("GenServer received unexpected CastMessage");
201+
(false, state_clone)
202+
}
193203
},
194204
Err(error) => {
195205
tracing::trace!("Error in callback, reverting state - Error: '{error:?}'");
@@ -207,15 +217,19 @@ where
207217

208218
fn handle_call(
209219
&mut self,
210-
message: Self::CallMsg,
211-
handle: &GenServerHandle<Self>,
212-
state: Self::State,
213-
) -> CallResponse<Self>;
220+
_message: Self::CallMsg,
221+
_handle: &GenServerHandle<Self>,
222+
_state: Self::State,
223+
) -> CallResponse<Self> {
224+
CallResponse::Unused
225+
}
214226

215227
fn handle_cast(
216228
&mut self,
217-
message: Self::CastMsg,
218-
handle: &GenServerHandle<Self>,
219-
state: Self::State,
220-
) -> CastResponse<Self>;
229+
_message: Self::CastMsg,
230+
_handle: &GenServerHandle<Self>,
231+
_state: Self::State,
232+
) -> CastResponse<Self> {
233+
CastResponse::Unused
234+
}
221235
}

concurrency/src/threads/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! spawned concurrency
22
//! IO threads-based traits and structs to implement concurrent code à-la-Erlang.
33
4-
mod error;
54
mod gen_server;
65
mod process;
76
mod time;

examples/bank/src/server.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use std::collections::HashMap;
22

3-
use spawned_concurrency::tasks::{CallResponse, CastResponse, GenServer, GenServerHandle};
3+
use spawned_concurrency::{
4+
messages::Unused,
5+
tasks::{CallResponse, GenServer, GenServerHandle},
6+
};
47

58
use crate::messages::{BankError, BankInMessage as InMessage, BankOutMessage as OutMessage};
69

@@ -42,7 +45,7 @@ impl Bank {
4245

4346
impl GenServer for Bank {
4447
type CallMsg = InMessage;
45-
type CastMsg = ();
48+
type CastMsg = Unused;
4649
type OutMsg = MsgResult;
4750
type Error = BankError;
4851
type State = BankState;
@@ -117,13 +120,4 @@ impl GenServer for Bank {
117120
Self::CallMsg::Stop => CallResponse::Stop(Ok(OutMessage::Stopped)),
118121
}
119122
}
120-
121-
async fn handle_cast(
122-
&mut self,
123-
_message: Self::CastMsg,
124-
_handle: &BankHandle,
125-
state: Self::State,
126-
) -> CastResponse<Self> {
127-
CastResponse::NoReply(state)
128-
}
129123
}

0 commit comments

Comments
 (0)