pub struct Sender<T> { /* private fields */ }
Available on
unstable
only.Expand description
The sending side of a channel.
This struct is created by the channel
function. See its
documentation for more.
§Examples
use async_std::sync::channel;
use async_std::task;
let (s1, r) = channel(100);
let s2 = s1.clone();
task::spawn(async move { s1.send(1).await });
task::spawn(async move { s2.send(2).await });
let msg1 = r.recv().await.unwrap();
let msg2 = r.recv().await.unwrap();
assert_eq!(msg1 + msg2, 3);
Implementations§
Source§impl<T> Sender<T>
impl<T> Sender<T>
Sourcepub async fn send(&self, msg: T)
pub async fn send(&self, msg: T)
Sends a message into the channel.
If the channel is full, this method will wait until there is space in the channel.
§Examples
use async_std::sync::channel;
use async_std::task;
let (s, r) = channel(1);
task::spawn(async move {
s.send(1).await;
s.send(2).await;
});
assert_eq!(r.recv().await, Some(1));
assert_eq!(r.recv().await, Some(2));
assert_eq!(r.recv().await, None);
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the channel capacity.
§Examples
use async_std::sync::channel;
let (s, _) = channel::<i32>(5);
assert_eq!(s.capacity(), 5);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the channel is empty.
§Examples
use async_std::sync::channel;
let (s, r) = channel(1);
assert!(s.is_empty());
s.send(0).await;
assert!(!s.is_empty());
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Sender<T>
impl<T> !RefUnwindSafe for Sender<T>
impl<T> Send for Sender<T>where
T: Send,
impl<T> Sync for Sender<T>where
T: Send,
impl<T> Unpin for Sender<T>
impl<T> !UnwindSafe for Sender<T>
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