pub struct Recipient<M>{ /* private fields */ }
Expand description
The Recipient
type allows to send one specific message to an actor.
You can get a recipient using the Addr::recipient()
method. It is possible
to use the Clone::clone()
method to get a cloned recipient.
Implementations§
source§impl<M> Recipient<M>
impl<M> Recipient<M>
sourcepub fn do_send(&self, msg: M)
pub fn do_send(&self, msg: M)
Sends a message.
The message is always queued, even if the mailbox for the receiver is full. If the mailbox is closed, the message is silently dropped.
Examples found in repository?
examples/weak_recipient.rs (line 29)
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
fn send_tick(&mut self, _ctx: &mut Context<Self>) {
for client in self.clients.iter() {
if let Some(client) = client.upgrade() {
client.do_send(TimePing(Instant::now()));
println!("⏰ sent ping to client {:?}", client);
} else {
println!("⏰ client can no longer be upgraded");
}
}
// gc
self.clients = self
.clients
.drain(..)
.filter(|c| c.upgrade().is_some())
.collect();
println!("⏰ service has {} clients", self.clients.len());
}
More examples
examples/ring.rs (line 54)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
fn handle(&mut self, msg: Payload, _: &mut Context<Self>) {
if msg.0 >= self.limit {
println!(
"Actor {} reached limit of {} (payload was {})",
self.id, self.limit, msg.0
);
System::current().stop();
return;
}
// Some prime in order for different actors to report progress.
// Large enough to print about once per second in debug mode.
if msg.0 % 498989 == 1 {
println!(
"Actor {} received message {} of {} ({:.2}%)",
self.id,
msg.0,
self.limit,
100.0 * msg.0 as f32 / self.limit as f32
);
}
self.next.do_send(Payload(msg.0 + 1));
}
sourcepub fn try_send(&self, msg: M) -> Result<(), SendError<M>>
pub fn try_send(&self, msg: M) -> Result<(), SendError<M>>
Attempts to send a message.
This method fails if the actor’s mailbox is full or closed. This method registers the current task in the receivers queue.
sourcepub fn send(&self, msg: M) -> RecipientRequest<M>
pub fn send(&self, msg: M) -> RecipientRequest<M>
Sends a message and asynchronously wait for a response.
The communication channel to the actor is bounded. If the returned RecipientRequest
object
gets dropped, the message is cancelled.
pub fn connected(&self) -> bool
sourcepub fn downgrade(&self) -> WeakRecipient<M>
pub fn downgrade(&self) -> WeakRecipient<M>
Returns a downgraded WeakRecipient
Trait Implementations§
source§impl<M> From<Recipient<M>> for WeakRecipient<M>
impl<M> From<Recipient<M>> for WeakRecipient<M>
source§impl<M> PartialEq for Recipient<M>
impl<M> PartialEq for Recipient<M>
impl<M> Eq for Recipient<M>
Auto Trait Implementations§
impl<M> !RefUnwindSafe for Recipient<M>
impl<M> Send for Recipient<M>
impl<M> Sync for Recipient<M>
impl<M> Unpin for Recipient<M>
impl<M> !UnwindSafe for Recipient<M>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more