diff --git a/concurrency/src/tasks/gen_server.rs b/concurrency/src/tasks/gen_server.rs index 5d42603..7eb6795 100644 --- a/concurrency/src/tasks/gen_server.rs +++ b/concurrency/src/tasks/gen_server.rs @@ -120,7 +120,7 @@ pub enum CastResponse { pub trait GenServer where - Self: Send + Sized, + Self: Default + Send + Sized, { type CallMsg: Clone + Send + Sized + Sync; type CastMsg: Clone + Send + Sized + Sync; @@ -128,7 +128,9 @@ where type State: Clone + Send; type Error: Debug + Send; - fn new() -> Self; + fn new() -> Self { + Self::default() + } fn start(initial_state: Self::State) -> GenServerHandle { GenServerHandle::new(initial_state) @@ -307,6 +309,8 @@ mod tests { use super::*; use crate::tasks::send_after; use std::{thread, time::Duration}; + + #[derive(Default)] struct BadlyBehavedTask; #[derive(Clone)] @@ -326,10 +330,6 @@ mod tests { type State = (); type Error = (); - fn new() -> Self { - Self {} - } - async fn handle_call( &mut self, _: Self::CallMsg, @@ -351,6 +351,7 @@ mod tests { } } + #[derive(Default)] struct WellBehavedTask; #[derive(Clone)] @@ -365,10 +366,6 @@ mod tests { type State = CountState; type Error = (); - fn new() -> Self { - Self {} - } - async fn handle_call( &mut self, message: Self::CallMsg, diff --git a/concurrency/src/tasks/stream_tests.rs b/concurrency/src/tasks/stream_tests.rs index e96c7e1..74e5f35 100644 --- a/concurrency/src/tasks/stream_tests.rs +++ b/concurrency/src/tasks/stream_tests.rs @@ -8,6 +8,7 @@ use crate::tasks::{ type SummatoryHandle = GenServerHandle; +#[derive(Default)] struct Summatory; type SummatoryState = u16; @@ -32,10 +33,6 @@ impl GenServer for Summatory { type State = SummatoryState; type Error = (); - fn new() -> Self { - Self - } - async fn handle_cast( &mut self, message: Self::CastMsg, diff --git a/concurrency/src/tasks/timer_tests.rs b/concurrency/src/tasks/timer_tests.rs index 297a45c..d02f9cf 100644 --- a/concurrency/src/tasks/timer_tests.rs +++ b/concurrency/src/tasks/timer_tests.rs @@ -28,6 +28,7 @@ enum RepeaterOutMessage { Count(i32), } +#[derive(Default)] struct Repeater; impl Repeater { @@ -53,10 +54,6 @@ impl GenServer for Repeater { type State = RepeaterState; type Error = (); - fn new() -> Self { - Self - } - async fn init( &mut self, handle: &RepeaterHandle, @@ -157,6 +154,7 @@ enum DelayedOutMessage { Count(i32), } +#[derive(Default)] struct Delayed; impl Delayed { @@ -179,10 +177,6 @@ impl GenServer for Delayed { type State = DelayedState; type Error = (); - fn new() -> Self { - Self - } - async fn handle_call( &mut self, message: Self::CallMsg, diff --git a/concurrency/src/threads/gen_server.rs b/concurrency/src/threads/gen_server.rs index 753e849..9ace83e 100644 --- a/concurrency/src/threads/gen_server.rs +++ b/concurrency/src/threads/gen_server.rs @@ -83,7 +83,7 @@ pub enum CastResponse { pub trait GenServer where - Self: Send + Sized, + Self: Default + Send + Sized, { type CallMsg: Clone + Send + Sized; type CastMsg: Clone + Send + Sized; @@ -91,7 +91,9 @@ where type State: Clone + Send; type Error: Debug; - fn new() -> Self; + fn new() -> Self { + Self::default() + } fn start(initial_state: Self::State) -> GenServerHandle { GenServerHandle::new(initial_state) diff --git a/concurrency/src/threads/timer_tests.rs b/concurrency/src/threads/timer_tests.rs index 6b3b8a4..f5d2124 100644 --- a/concurrency/src/threads/timer_tests.rs +++ b/concurrency/src/threads/timer_tests.rs @@ -28,6 +28,7 @@ enum RepeaterOutMessage { Count(i32), } +#[derive(Default)] struct Repeater; impl Repeater { @@ -47,10 +48,6 @@ impl GenServer for Repeater { type State = RepeaterState; type Error = (); - fn new() -> Self { - Self - } - fn init( &mut self, handle: &RepeaterHandle, @@ -147,6 +144,7 @@ enum DelayedOutMessage { Count(i32), } +#[derive(Default)] struct Delayed; impl Delayed { @@ -162,10 +160,6 @@ impl GenServer for Delayed { type State = DelayedState; type Error = (); - fn new() -> Self { - Self - } - fn handle_call( &mut self, _message: Self::CallMsg, diff --git a/examples/bank/src/server.rs b/examples/bank/src/server.rs index 7a39a7c..de5bbb7 100644 --- a/examples/bank/src/server.rs +++ b/examples/bank/src/server.rs @@ -11,6 +11,7 @@ type MsgResult = Result; type BankHandle = GenServerHandle; type BankState = HashMap; +#[derive(Default)] pub struct Bank {} impl Bank { @@ -50,10 +51,6 @@ impl GenServer for Bank { type Error = BankError; type State = BankState; - fn new() -> Self { - Self {} - } - // Initializing "main" account with 1000 in balance to test init() callback. async fn init( &mut self, diff --git a/examples/bank_threads/src/server.rs b/examples/bank_threads/src/server.rs index e79cf90..610099e 100644 --- a/examples/bank_threads/src/server.rs +++ b/examples/bank_threads/src/server.rs @@ -11,6 +11,7 @@ type MsgResult = Result; type BankHandle = GenServerHandle; type BankState = HashMap; +#[derive(Default)] pub struct Bank {} impl Bank { @@ -46,10 +47,6 @@ impl GenServer for Bank { type Error = BankError; type State = BankState; - fn new() -> Self { - Self {} - } - // Initializing "main" account with 1000 in balance to test init() callback. fn init( &mut self, diff --git a/examples/blocking_genserver/main.rs b/examples/blocking_genserver/main.rs index 8dead78..51f4138 100644 --- a/examples/blocking_genserver/main.rs +++ b/examples/blocking_genserver/main.rs @@ -6,7 +6,8 @@ use spawned_concurrency::tasks::{ CallResponse, CastResponse, GenServer, GenServerHandle, send_after, }; -// We test a scenario with a badly behaved task +// We test a scenario with a badly behaved task#[derive(Default)] +#[derive(Default)] struct BadlyBehavedTask; #[derive(Clone)] @@ -26,10 +27,6 @@ impl GenServer for BadlyBehavedTask { type State = (); type Error = (); - fn new() -> Self { - Self {} - } - async fn handle_call( &mut self, _: Self::CallMsg, @@ -53,6 +50,7 @@ impl GenServer for BadlyBehavedTask { } } +#[derive(Default)] struct WellBehavedTask; #[derive(Clone)] @@ -67,10 +65,6 @@ impl GenServer for WellBehavedTask { type State = CountState; type Error = (); - fn new() -> Self { - Self {} - } - async fn handle_call( &mut self, message: Self::CallMsg, diff --git a/examples/name_server/src/server.rs b/examples/name_server/src/server.rs index 39c6dba..2191c32 100644 --- a/examples/name_server/src/server.rs +++ b/examples/name_server/src/server.rs @@ -10,6 +10,7 @@ use crate::messages::{NameServerInMessage as InMessage, NameServerOutMessage as type NameServerHandle = GenServerHandle; type NameServerState = HashMap; +#[derive(Default)] pub struct NameServer {} impl NameServer { @@ -35,10 +36,6 @@ impl GenServer for NameServer { type Error = std::fmt::Error; type State = NameServerState; - fn new() -> Self { - Self {} - } - async fn handle_call( &mut self, message: Self::CallMsg, diff --git a/examples/name_server_with_error/src/server.rs b/examples/name_server_with_error/src/server.rs index fc05ce7..def8ecf 100644 --- a/examples/name_server_with_error/src/server.rs +++ b/examples/name_server_with_error/src/server.rs @@ -11,6 +11,7 @@ use crate::messages::{NameServerInMessage as InMessage, NameServerOutMessage as type NameServerHandle = GenServerHandle; type NameServerState = HashMap; +#[derive(Default)] pub struct NameServer {} impl NameServer { @@ -37,10 +38,6 @@ impl GenServer for NameServer { type Error = std::fmt::Error; type State = NameServerState; - fn new() -> Self { - NameServer {} - } - async fn handle_call( &mut self, message: Self::CallMsg, diff --git a/examples/updater/src/server.rs b/examples/updater/src/server.rs index a32fd3a..fbb822e 100644 --- a/examples/updater/src/server.rs +++ b/examples/updater/src/server.rs @@ -16,6 +16,7 @@ pub struct UpdateServerState { pub periodicity: Duration, pub timer_token: Option, } +#[derive(Default)] pub struct UpdaterServer {} impl GenServer for UpdaterServer { @@ -25,10 +26,6 @@ impl GenServer for UpdaterServer { type Error = std::fmt::Error; type State = UpdateServerState; - fn new() -> Self { - Self {} - } - // Initializing GenServer to start periodic checks. async fn init( &mut self, diff --git a/examples/updater_threads/src/server.rs b/examples/updater_threads/src/server.rs index c42ab68..ace3be0 100644 --- a/examples/updater_threads/src/server.rs +++ b/examples/updater_threads/src/server.rs @@ -15,6 +15,7 @@ pub struct UpdateServerState { pub url: String, pub periodicity: Duration, } +#[derive(Default)] pub struct UpdaterServer {} impl GenServer for UpdaterServer { @@ -24,10 +25,6 @@ impl GenServer for UpdaterServer { type Error = std::fmt::Error; type State = UpdateServerState; - fn new() -> Self { - Self {} - } - // Initializing GenServer to start periodic checks. fn init( &mut self,