use std::marker::PhantomData;
use tokio::sync::mpsc;
use crate::error::{Result, RiftError};
#[derive(Debug)]
pub struct LocalActorRef<M> {
tx: mpsc::Sender<M>,
_phantom: PhantomData<M>,
}
impl<M> Clone for LocalActorRef<M> {
fn clone(&self) -> Self {
Self {
tx: self.tx.clone(),
_phantom: PhantomData,
}
}
}
impl<M> LocalActorRef<M> {
pub fn new(tx: mpsc::Sender<M>) -> Self {
Self {
tx,
_phantom: PhantomData,
}
}
pub fn send(&self, msg: M) -> Result<()> {
self.tx.try_send(msg).map_err(|_| {
RiftError::System(crate::error::SystemReject::Internal("actor died".into()))
})
}
pub fn is_closed(&self) -> bool {
self.tx.is_closed()
}
pub fn sender(&self) -> mpsc::Sender<M> {
self.tx.clone()
}
}
pub struct RemoteActorRef<M> {
_phantom: PhantomData<M>,
}
impl<M> RemoteActorRef<M> {
pub fn new() -> Self {
Self {
_phantom: PhantomData,
}
}
}
impl<M> Default for RemoteActorRef<M> {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn local_ref_send_and_is_closed() {
let (tx, rx) = mpsc::channel::<i32>(1);
let r = LocalActorRef::new(tx);
assert!(!r.is_closed());
r.send(42).unwrap();
drop(rx);
assert!(r.is_closed());
}
}