core::channel
本页内容
API 参考只有英文版,由标准库源代码中的英文文档注释生成。
message passing between threads.
Multi-producer, multi-consumer: a Receiver copy
shares the queue, and each value goes to one receiver. recv / send
block the OS thread, recv_async / for await / send_async suspend
the task (task::park: receivers on the meta address, senders one
past it).
Structs
EmptySource
pub struct Emptytry_recv: nothing is queued, and a sender is still open.
ClosedSource
pub struct Closedtry_recv: nothing is queued, and every sender has closed.
SenderSource
pub struct Sender<T>The sending end. Each clone is its own sender and is closed on its own; the channel closes when every sender has closed.
Queue
value(blocking while a bounded channel is full, and on a rendezvous channel until it is received). Throws it back if this sender was closed.
Sourcepub fn try_send(§self, value: T) throws (TrySendError<T>) Queue
valuewithout blocking:Fullif a bounded channel is full, or if no receiver is waiting on a rendezvous channel.
Sourcepub const fn send_async(§self, value: T) -> SendFuture<T> try await tx.send_async(v)inside async code: likesend, but suspends the task (not the thread) while a bounded channel is full, and on a rendezvous channel until the value is received.Close this sender; later
sends on it throwClosed.
ReceiverSource
pub struct Receiver<T>The receiving end.
Sourcepub const fn recv_async(§self) -> RecvFuture<T> await rx.recv_async()inside async code: likerecv, but suspends the task instead of blocking the thread.The next value, blocking until one arrives;
noneonce the queue is empty and every sender has closed.The next value if one is queued, else whether a sender may still send one (
Empty) or every sender has closed (Closed).
Sourcepub fn iter(§self) -> ReceiverIter<T> Values until the channel closes (also
for v in rx).
Trait implementations 3
impl<T> Debug for Receiver<T>impl<T> IntoIterator for Receiver<T>impl<T> core::stream::Stream for Receiver<T>
ReceiverIterSource
pub struct ReceiverIter<T>The iterator for x in rx walks: each item is a blocking recv; it
ends when the channel is closed and drained.
RecvFutureSource
pub struct RecvFuture<T>Receiver::recv_async.
SendFutureSource
pub struct SendFuture<T>Sender::send_async.
Sourcepub fn poll(§ref self, ref ctx: core::future::Context) throws (SendError<T>) -> core::future::Poll<()> One step of an async send:
Ready(())once the value is queued,Pendingwhile the channel is full. ThrowsSendError::Closedwith the value when this sender was closed.
Trait implementations 1
impl<T> Debug for SendFuture<T>
Enums
SendErrorSource
pub enum SendError<T>What send hands back when this sender was closed.
What went wrong, for display.
TrySendErrorSource
pub enum TrySendError<T>What try_send hands back when the value was not sent: the channel
was full, or this sender was closed.
What went wrong, for display.
Functions
channelSource
pub const fn channel<T: Send>() -> (Sender<T>, Receiver<T>) An unbounded channel.
channel_boundedSource
pub const fn channel_bounded<T: Send>(cap: usize) -> (Sender<T>, Receiver<T>) A channel holding at most cap queued values: send blocks while
it is full. cap 0 is a rendezvous channel: send returns once a
receiver has taken the value.