Struct aldrin::UnclaimedSender
source · pub struct UnclaimedSender<T: ?Sized> { /* private fields */ }
Expand description
A sender that hasn’t been claimed yet.
UnclaimedSender
s are similar to UnboundSender
s in that they
identify the sending end of a channel in an unclaimed state. This sender is however bound to a
client and can thus be claimed.
Implementations§
source§impl<T: ?Sized> UnclaimedSender<T>
impl<T: ?Sized> UnclaimedSender<T>
Returns the sender’s ChannelCookie
.
sourcepub fn client(&self) -> Option<&Handle>
pub fn client(&self) -> Option<&Handle>
Returns a handle to the client that was used to create the sender.
None
will be returned if the sender is closed.
sourcepub fn unbind(self) -> UnboundSender<T>
pub fn unbind(self) -> UnboundSender<T>
Unbinds the sender 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 sender) to another client, it must first be unbound from its client.
§Examples
// Create a channel with an unclaimed sender and a claimed receiver.
let (sender, receiver) = handle.create_channel_with_claimed_receiver::<u32>(16).await?;
// Unbind the sender so that it can be sent to another client. This will typically happen by
// returning it from a function call.
let sender = sender.unbind();
sourcepub async fn close(&mut self) -> Result<(), Error>
pub async fn close(&mut self) -> Result<(), Error>
Closes the sender without consuming it.
This closes the sender such that it cannot be claimed anymore by any client. When the receiver waits for the channel to become established, an error will be returned.
After closing a sender, any further function calls will return Error::InvalidChannel
.
§Examples
use aldrin::Error;
let (mut sender, receiver) = handle.create_channel_with_claimed_receiver::<u32>(16).await?;
// Close the sender.
sender.close().await?;
// For the receiver, an error will be returned when waiting for the channel to become
// established.
let err = receiver.established().await.unwrap_err();
assert_eq!(err, Error::InvalidChannel);
sourcepub async fn claim(self) -> Result<Sender<T>, Error>
pub async fn claim(self) -> Result<Sender<T>, Error>
Claims the sender 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 sender, must be claimed manually (after it has possibly been sent to another client).
When this function returns successfully, a receiver’s call to
PendingReceiver::established
will also resolve successfully.
This function can fail in the following cases:
- Some other client has already claimed the sender.
- Some other client has closed the sender.
- The receiver has been closed.
§Examples
// The sender is unclaimed, while the receiver has been claimed automatically.
let (sender, receiver) = handle.create_channel_with_claimed_receiver(16).await?;
// Claim the sender.
let mut sender = sender.claim().await?;
// This will now resolve immediately.
let mut receiver = receiver.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: ?Sized>(self) -> UnclaimedSender<U>
pub fn cast<U: ?Sized>(self) -> UnclaimedSender<U>
Casts the item type to a different type.