Struct Sender

Source
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>

Source

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);
Source

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);
Source

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());
Source

pub fn is_full(&self) -> bool

Returns true if the channel is full.

§Examples
use async_std::sync::channel;

let (s, r) = channel(1);

assert!(!s.is_full());
s.send(0).await;
assert!(s.is_full());
Source

pub fn len(&self) -> usize

Returns the number of messages in the channel.

§Examples
use async_std::sync::channel;

let (s, r) = channel(2);
assert_eq!(s.len(), 0);

s.send(1).await;
s.send(2).await;
assert_eq!(s.len(), 2);

Trait Implementations§

Source§

impl<T> Clone for Sender<T>

Source§

fn clone(&self) -> Sender<T>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for Sender<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Drop for Sender<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.