use std::sync::Arc;
use super::actor_ref::UntypedActorRef;
use super::path::ActorPath;
use super::remote::RemoteRef;
#[derive(Clone)]
#[non_exhaustive]
#[derive(Default)]
pub enum Sender {
Local(UntypedActorRef),
Remote {
path: ActorPath,
handle: Arc<dyn RemoteRef>,
},
#[default]
None,
}
impl Sender {
pub fn path(&self) -> Option<&ActorPath> {
match self {
Sender::Local(r) => Some(r.path()),
Sender::Remote { path, .. } => Some(path),
Sender::None => None,
}
}
pub fn is_remote(&self) -> bool {
matches!(self, Sender::Remote { .. })
}
pub fn is_none(&self) -> bool {
matches!(self, Sender::None)
}
pub fn local(&self) -> Option<&UntypedActorRef> {
if let Sender::Local(r) = self {
Some(r)
} else {
None
}
}
}
impl std::fmt::Debug for Sender {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Sender::Local(r) => f.debug_tuple("Local").field(&r.path().to_string()).finish(),
Sender::Remote { path, .. } => f.debug_struct("Remote").field("path", &path.to_string()).finish(),
Sender::None => f.write_str("None"),
}
}
}
impl From<UntypedActorRef> for Sender {
fn from(r: UntypedActorRef) -> Self {
Sender::Local(r)
}
}
impl<M: Send + 'static> From<&super::actor_ref::ActorRef<M>> for Sender {
fn from(r: &super::actor_ref::ActorRef<M>) -> Self {
Sender::Local(r.as_untyped())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn none_is_default() {
let s = Sender::default();
assert!(s.is_none());
assert!(!s.is_remote());
assert!(s.path().is_none());
assert!(s.local().is_none());
}
}