use std::future::{self, Future};
use crate::actor::{Actor, ActorContext};
use crate::message::{Handler, Message};
use crate::utils::debug_trace;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Signal {
Stop,
Terminate,
}
impl Message for Signal {
type Result = ();
}
impl TryFrom<u8> for Signal {
type Error = ();
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
0 => Ok(Signal::Stop),
1 => Ok(Signal::Terminate),
_ => Err(()),
}
}
}
impl<A> Handler<Signal> for A
where
A: Actor,
A::Context: ActorContext<A>,
{
type Result = ();
fn handle(
&mut self,
msg: Signal,
ctx: &mut Self::Context,
) -> impl Future<Output = Self::Result> + Send {
debug_trace!("Handle command {:?}", msg);
match msg {
Signal::Stop => {
ctx.stop();
}
Signal::Terminate => {
ctx.terminate();
}
}
future::ready(())
}
}
#[cfg(feature = "identifier")]
impl crate::stable_type_id::HasStableTypeId for Signal {
const STABLE_TYPE_ID: crate::stable_type_id::StableTypeId =
crate::stable_type_id::StableTypeId::from_stable_type_name(concat!(
module_path!(),
"::",
"Signal"
));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_signal_try_from() {
assert_eq!(Signal::try_from(0), Ok(Signal::Stop));
assert_eq!(Signal::try_from(1), Ok(Signal::Terminate));
assert_eq!(Signal::try_from(2), Err(()));
}
}