use crate::{ import::*, Addr, addr_inner::*, error::*, ActorInfo };
pub struct WeakAddr< A: Actor >
{
inner: AddrInner<A>,
}
impl< A: Actor > Clone for WeakAddr<A>
{
fn clone( &self ) -> Self
{
let _s = self.info().span().entered();
trace!( "CREATE WeakAddr" );
Self
{
inner: self.inner.clone(),
}
}
}
impl< A: Actor > PartialEq for WeakAddr<A>
{
fn eq( &self, other: &Self ) -> bool
{
self.inner == other.inner
}
}
impl< A: Actor > Eq for WeakAddr<A>{}
impl<A: Actor> fmt::Debug for WeakAddr<A>
{
fn fmt( &self, f: &mut fmt::Formatter<'_> ) -> fmt::Result
{
let name = match &self.name().is_empty()
{
true => String::new(),
false => format!( ", {}", &self.name() )
};
write!
(
f ,
"WeakAddr<{}> ~ {}{}" ,
std::any::type_name::<A>() ,
&self.id() ,
name ,
)
}
}
impl<A: Actor> fmt::Display for WeakAddr<A>
{
fn fmt( &self, f: &mut fmt::Formatter<'_> ) -> fmt::Result
{
write!( f, "{} ({}, {})", self.inner.type_name(), self.id(), &self.name() )
}
}
impl<A> WeakAddr<A> where A: Actor
{
pub fn strong( &self ) -> Result< Addr<A>, ThesErr >
{
Addr::try_from( self.inner.clone() )
}
pub fn info( &self ) -> Arc<ActorInfo>
{
self.inner.info.clone()
}
}
impl<A: Actor> Drop for WeakAddr<A>
{
fn drop( &mut self )
{
let _s = self.info().span().entered();
trace!( "DROP WeakAddr" );
}
}
impl<A, M> Address<M> for WeakAddr<A>
where A: Actor + Handler<M> ,
M: Message ,
{
fn call( &mut self, msg: M ) -> Return<'_, ThesRes< <M as Message>::Return >>
{
self.inner.call( msg )
}
fn clone_box( &self ) -> BoxAddress<M, ThesErr>
{
Box::new( self.clone() )
}
}
impl<A> Identify for WeakAddr<A>
where A: Actor,
{
fn id( &self ) -> usize
{
self.inner.id()
}
fn name( &self ) -> Arc<str>
{
self.inner.name()
}
}
impl<A, M> Sink<M> for WeakAddr<A>
where A: Actor + Handler<M> ,
M: Message ,
{
type Error = ThesErr;
fn poll_ready( mut self: Pin<&mut Self>, cx: &mut TaskContext<'_> ) -> Poll<Result<(), Self::Error>>
{
if self.inner.strong.lock().expect( "Mutex<StrongCount> poisoned" ).count() == 0
{
return Poll::Ready( Err( ThesErr::MailboxClosed{ info: self.inner.info.clone(), src: None } ) )
}
Pin::new( &mut self.inner ).poll_ready( cx )
}
fn start_send( mut self: Pin<&mut Self>, msg: M ) -> Result<(), Self::Error>
{
Pin::new( &mut self.inner ).start_send( msg )
}
fn poll_flush( mut self: Pin<&mut Self>, cx: &mut TaskContext<'_> ) -> Poll<Result<(), Self::Error>>
{
Pin::new( &mut self.inner ).poll_flush( cx )
}
fn poll_close( mut self: Pin<&mut Self>, cx: &mut TaskContext<'_> ) -> Poll<Result<(), Self::Error>>
{
Pin::new( &mut self.inner ).poll_flush( cx )
}
}
impl<A: Actor> From< AddrInner<A> > for WeakAddr<A>
{
fn from( inner: AddrInner<A> ) -> Self
{
Self{ inner }
}
}