use std::time::{Duration, Instant};
use super::{Actor, Handler, Message};
#[derive(Debug, Clone, Copy)]
pub struct Ping(Instant);
impl Message for Ping {
type Response = Duration;
}
impl Ping {
pub fn now() -> Self {
Ping(Instant::now())
}
}
impl<A: Actor> Handler<Ping> for A {
async fn handle(&mut self, _ctx: &mut crate::Context<Self>, Ping(sent_time): Ping) -> Duration {
Instant::now() - sent_time
}
}
#[derive(Debug, Clone, Copy)]
pub struct GC;
impl Message for GC {
type Response = ();
}
impl<A: Actor> Handler<GC> for A {
async fn handle(&mut self, ctx: &mut crate::Context<Self>, _: GC) {
log::trace!("Garbage Collecting");
ctx.gc();
}
}
#[derive(Debug, Clone, Copy)]
pub struct Stop;
impl Message for Stop {
type Response = crate::error::Result<()>;
}
impl<A: Actor> Handler<Stop> for A {
async fn handle(
&mut self,
ctx: &mut crate::Context<Self>,
_: Stop,
) -> crate::error::Result<()> {
ctx.stop()
}
}