#![allow(refining_impl_trait)]
pub mod actor;
pub mod error;
pub mod handler;
pub mod network;
pub mod processor;
pub mod runtime;
pub mod prelude {
pub use crate::{
actor::LifeCycle,
handler::{Handler, Message},
network::{Network, Socket},
};
}
#[cfg(any(test, feature = "fixtures"))]
pub mod fixtures {
use crate::prelude::*;
#[derive(Debug, Clone)]
pub struct Ping;
#[derive(Debug, Clone)]
pub struct Pong;
#[derive(Debug, Clone)]
pub struct Counter {
pub count: usize,
}
impl LifeCycle for Counter {
type Snapshot = usize;
type StartMessage = ();
type StopMessage = ();
fn on_start(&mut self) -> Self::StartMessage {}
fn on_stop(&mut self) -> Self::StopMessage {}
fn snapshot(&self) -> Self::Snapshot { self.count }
}
impl Handler<Ping> for Counter {
type Reply = ();
fn handle(&mut self, _message: &Ping) -> Option<Self::Reply> {
self.count += 1;
tracing::debug!(count = self.count, "Counter received Ping");
None
}
}
impl Handler<Pong> for Counter {
type Reply = ();
fn handle(&mut self, _message: &Pong) -> Option<Self::Reply> {
self.count += 1;
tracing::debug!(count = self.count, "Counter received Pong");
None
}
}
#[derive(Debug, Clone)]
pub struct PingPlayer {
pub count: usize,
pub max_count: usize,
}
impl LifeCycle for PingPlayer {
type Snapshot = usize;
type StartMessage = Ping;
type StopMessage = ();
fn on_start(&mut self) -> Self::StartMessage { Ping }
fn on_stop(&mut self) -> Self::StopMessage {}
fn snapshot(&self) -> Self::Snapshot { self.count }
fn should_stop(&self) -> bool { self.count >= self.max_count }
}
impl Handler<Pong> for PingPlayer {
type Reply = Ping;
fn handle(&mut self, _message: &Pong) -> Option<Self::Reply> {
self.count += 1;
Some(Ping)
}
}
#[derive(Debug, Clone)]
pub struct PongPlayer;
impl LifeCycle for PongPlayer {
type Snapshot = ();
type StartMessage = ();
type StopMessage = ();
fn on_start(&mut self) -> Self::StartMessage {}
fn on_stop(&mut self) -> Self::StopMessage {}
fn snapshot(&self) -> Self::Snapshot {}
}
impl Handler<Ping> for PongPlayer {
type Reply = Pong;
fn handle(&mut self, _message: &Ping) -> Option<Self::Reply> { Some(Pong) }
}
}