Struct std::comm::SenderUnstable
[-]
[+]
[src]
pub struct Sender<T> { // some fields omitted }
The sending-half of Rust's asynchronous channel type. This half can only be owned by one task, but it can be cloned to send to other tasks.
Methods
impl<T: Send> Sender<T>
fn send(&self, t: T)
Sends a value along this channel to be received by the corresponding receiver.
Rust channels are infinitely buffered so this method will never block.
Panics
This function will panic if the other end of the channel has hung up. This means that if the corresponding receiver has fallen out of scope, this function will trigger a panic message saying that a message is being sent on a closed channel.
Note that if this function does not panic, it does not mean that the data will be successfully received. All sends are placed into a queue, so it is possible for a send to succeed (the other end is alive), but then the other end could immediately disconnect.
The purpose of this functionality is to propagate panics among tasks.
If a panic is not desired, then consider using the send_opt
method
fn send_opt(&self, t: T) -> Result<(), T>
Attempts to send a value on this channel, returning it back if it could not be sent.
A successful send occurs when it is determined that the other end of
the channel has not hung up already. An unsuccessful send would be one
where the corresponding receiver has already been deallocated. Note
that a return value of Err
means that the data will never be
received, but a return value of Ok
does not mean that the data
will be received. It is possible for the corresponding receiver to
hang up immediately after this function returns Ok
.
Like send
, this method will never block.
Panics
This method will never panic, it will return the message back to the caller if the other end is disconnected
Example
fn main() { let (tx, rx) = channel(); // This send is always successful assert_eq!(tx.send_opt(1i), Ok(())); // This send will fail because the receiver is gone drop(rx); assert_eq!(tx.send_opt(1i), Err(1)); }let (tx, rx) = channel(); // This send is always successful assert_eq!(tx.send_opt(1i), Ok(())); // This send will fail because the receiver is gone drop(rx); assert_eq!(tx.send_opt(1i), Err(1));