Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions concurrency/src/tasks/gen_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,17 @@ pub enum CastResponse<G: GenServer> {

pub trait GenServer
where
Self: Send + Sized,
Self: Default + Send + Sized,
{
type CallMsg: Clone + Send + Sized + Sync;
type CastMsg: Clone + Send + Sized + Sync;
type OutMsg: Send + Sized;
type State: Clone + Send;
type Error: Debug + Send;

fn new() -> Self;
fn new() -> Self {
Self::default()
}

fn start(initial_state: Self::State) -> GenServerHandle<Self> {
GenServerHandle::new(initial_state)
Expand Down Expand Up @@ -307,6 +309,8 @@ mod tests {
use super::*;
use crate::tasks::send_after;
use std::{thread, time::Duration};

#[derive(Default)]
struct BadlyBehavedTask;

#[derive(Clone)]
Expand All @@ -326,10 +330,6 @@ mod tests {
type State = ();
type Error = ();

fn new() -> Self {
Self {}
}

async fn handle_call(
&mut self,
_: Self::CallMsg,
Expand All @@ -351,6 +351,7 @@ mod tests {
}
}

#[derive(Default)]
struct WellBehavedTask;

#[derive(Clone)]
Expand All @@ -365,10 +366,6 @@ mod tests {
type State = CountState;
type Error = ();

fn new() -> Self {
Self {}
}

async fn handle_call(
&mut self,
message: Self::CallMsg,
Expand Down
5 changes: 1 addition & 4 deletions concurrency/src/tasks/stream_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::tasks::{

type SummatoryHandle = GenServerHandle<Summatory>;

#[derive(Default)]
struct Summatory;

type SummatoryState = u16;
Expand All @@ -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,
Expand Down
10 changes: 2 additions & 8 deletions concurrency/src/tasks/timer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum RepeaterOutMessage {
Count(i32),
}

#[derive(Default)]
struct Repeater;

impl Repeater {
Expand All @@ -53,10 +54,6 @@ impl GenServer for Repeater {
type State = RepeaterState;
type Error = ();

fn new() -> Self {
Self
}

async fn init(
&mut self,
handle: &RepeaterHandle,
Expand Down Expand Up @@ -157,6 +154,7 @@ enum DelayedOutMessage {
Count(i32),
}

#[derive(Default)]
struct Delayed;

impl Delayed {
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions concurrency/src/threads/gen_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,17 @@ pub enum CastResponse<G: GenServer> {

pub trait GenServer
where
Self: Send + Sized,
Self: Default + Send + Sized,
{
type CallMsg: Clone + Send + Sized;
type CastMsg: Clone + Send + Sized;
type OutMsg: Send + Sized;
type State: Clone + Send;
type Error: Debug;

fn new() -> Self;
fn new() -> Self {
Self::default()
}

fn start(initial_state: Self::State) -> GenServerHandle<Self> {
GenServerHandle::new(initial_state)
Expand Down
10 changes: 2 additions & 8 deletions concurrency/src/threads/timer_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ enum RepeaterOutMessage {
Count(i32),
}

#[derive(Default)]
struct Repeater;

impl Repeater {
Expand All @@ -47,10 +48,6 @@ impl GenServer for Repeater {
type State = RepeaterState;
type Error = ();

fn new() -> Self {
Self
}

fn init(
&mut self,
handle: &RepeaterHandle,
Expand Down Expand Up @@ -147,6 +144,7 @@ enum DelayedOutMessage {
Count(i32),
}

#[derive(Default)]
struct Delayed;

impl Delayed {
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions examples/bank/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type MsgResult = Result<OutMessage, BankError>;
type BankHandle = GenServerHandle<Bank>;
type BankState = HashMap<String, i32>;

#[derive(Default)]
pub struct Bank {}

impl Bank {
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions examples/bank_threads/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type MsgResult = Result<OutMessage, BankError>;
type BankHandle = GenServerHandle<Bank>;
type BankState = HashMap<String, i32>;

#[derive(Default)]
pub struct Bank {}

impl Bank {
Expand Down Expand Up @@ -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,
Expand Down
12 changes: 3 additions & 9 deletions examples/blocking_genserver/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -26,10 +27,6 @@ impl GenServer for BadlyBehavedTask {
type State = ();
type Error = ();

fn new() -> Self {
Self {}
}

async fn handle_call(
&mut self,
_: Self::CallMsg,
Expand All @@ -53,6 +50,7 @@ impl GenServer for BadlyBehavedTask {
}
}

#[derive(Default)]
struct WellBehavedTask;

#[derive(Clone)]
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions examples/name_server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::messages::{NameServerInMessage as InMessage, NameServerOutMessage as
type NameServerHandle = GenServerHandle<NameServer>;
type NameServerState = HashMap<String, String>;

#[derive(Default)]
pub struct NameServer {}

impl NameServer {
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions examples/name_server_with_error/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::messages::{NameServerInMessage as InMessage, NameServerOutMessage as
type NameServerHandle = GenServerHandle<NameServer>;
type NameServerState = HashMap<String, String>;

#[derive(Default)]
pub struct NameServer {}

impl NameServer {
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions examples/updater/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub struct UpdateServerState {
pub periodicity: Duration,
pub timer_token: Option<CancellationToken>,
}
#[derive(Default)]
pub struct UpdaterServer {}

impl GenServer for UpdaterServer {
Expand All @@ -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,
Expand Down
5 changes: 1 addition & 4 deletions examples/updater_threads/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct UpdateServerState {
pub url: String,
pub periodicity: Duration,
}
#[derive(Default)]
pub struct UpdaterServer {}

impl GenServer for UpdaterServer {
Expand All @@ -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,
Expand Down