Struct aldrin::UnclaimedReceiver
source · pub struct UnclaimedReceiver<T: Deserialize> { /* private fields */ }
Expand description
A receiver that hasn’t been claimed yet.
UnclaimedReceiver
s are similar to UnboundReceiver
s in that they
identify the receiving end of a channel in an unclaimed state. This receiver is however bound to
a client and can thus be claimed.
Implementations§
source§impl<T: Deserialize> UnclaimedReceiver<T>
impl<T: Deserialize> UnclaimedReceiver<T>
sourcepub fn client(&self) -> Option<&Handle>
pub fn client(&self) -> Option<&Handle>
Returns a handle to the client that was used to create the receiver.
None
will be returned if the receiver is closed.
sourcepub fn unbind(self) -> UnboundReceiver<T>
pub fn unbind(self) -> UnboundReceiver<T>
Unbinds the receiver from its client.
When creating a channel, one end will already be claimed while the other end won’t. In order to send that other end (here the receiver) to another client, it must first be unbound from its client.
§Examples
// Create a channel with an unclaimed receiver and a claimed sender.
let (sender, receiver) = handle.create_channel_with_claimed_sender::<u32>().await?;
// Unbind the receiver so that it can be sent to another client. This will typically happen
// by returning it from a function call.
let receiver = receiver.unbind();
sourcepub async fn close(&mut self) -> Result<(), Error>
pub async fn close(&mut self) -> Result<(), Error>
Closes the receiver without consuming it.
This closes the receiver such that it cannot be claimed anymore by any client. When the sender waits for the channel to become established, an error will be returned.
After closing a receiver, any further function calls will return Error::InvalidChannel
.
§Examples
use aldrin::Error;
let (sender, mut receiver) = handle.create_channel_with_claimed_sender::<u32>().await?;
// Close the receiver.
receiver.close().await?;
// For the sender, an error will be returned when waiting for the channel to become
// established.
let err = sender.established().await.unwrap_err();
assert_eq!(err, Error::InvalidChannel);
sourcepub async fn claim(self, capacity: u32) -> Result<Receiver<T>, Error>
pub async fn claim(self, capacity: u32) -> Result<Receiver<T>, Error>
Claims the receiver by its bound client.
When creating channels, both ends must be claimed by a client before items can be sent and received. One end will always be claimed automatically. The other end, here the receiver, must be claimed manually (after it has possibly been sent to another client).
When this function returns successfully, a senders’s call to PendingSender::established
will also resolve successfully.
This function can fail in the following cases:
- Some other client has already claimed the receiver.
- Some other client has closed the receiver.
- The sender has been closed.
A capacity
of 0 is treated as if 1 was specificed instead.
§Examples
// The receiver is unclaimed, while the sender has been claimed automatically.
let (sender, receiver) = handle.create_channel_with_claimed_sender().await?;
// Claim the receiver.
let mut receiver = receiver.claim(16).await?;
// This will now resolve immediately.
let mut sender = sender.established().await?;
// The channel is now fully established and items can be sent and received.
sender.send_item(&1).await?;
sender.send_item(&2).await?;
assert_eq!(receiver.next_item().await, Ok(Some(1)));
assert_eq!(receiver.next_item().await, Ok(Some(2)));
sourcepub fn cast<U: Deserialize>(self) -> UnclaimedReceiver<U>
pub fn cast<U: Deserialize>(self) -> UnclaimedReceiver<U>
Casts the item type to a different type.