Struct Channel

pub(crate) struct Channel<T> { pub(in ::sync::mpmc::array) head: CachePadded<Atomic<usize>>, pub(in ::sync::mpmc::array) tail: CachePadded<Atomic<usize>>, pub(in ::sync::mpmc::array) buffer: Box<[Slot<T>]>, pub(in ::sync::mpmc::array) cap: usize, pub(in ::sync::mpmc::array) one_lap: usize, pub(in ::sync::mpmc::array) mark_bit: usize, pub(in ::sync::mpmc::array) senders: SyncWaker, pub(in ::sync::mpmc::array) receivers: SyncWaker }

Bounded channel based on a preallocated array.

Fields

head: CachePadded<Atomic<usize>>

The head of the channel.

This value is a "stamp" consisting of an index into the buffer, a mark bit, and a lap, but packed into a single usize. The lower bits represent the index, while the upper bits represent the lap. The mark bit in the head is always zero.

Messages are popped from the head of the channel.

tail: CachePadded<Atomic<usize>>

The tail of the channel.

This value is a "stamp" consisting of an index into the buffer, a mark bit, and a lap, but packed into a single usize. The lower bits represent the index, while the upper bits represent the lap. The mark bit indicates that the channel is disconnected.

Messages are pushed into the tail of the channel.

buffer: Box<[Slot<T>]>

The buffer holding slots.

cap: usize

The channel capacity.

one_lap: usize

A stamp with the value of { lap: 1, mark: 0, index: 0 }.

mark_bit: usize

If this bit is set in the tail, that means the channel is disconnected.

senders: SyncWaker

Senders waiting while the channel is full.

receivers: SyncWaker

Receivers waiting while the channel is empty and not disconnected.

Implementations

impl<T> Channel<T>

fn with_capacity(cap: usize) -> Self

Creates a bounded channel of capacity cap.

fn start_send(&self, token: &mut Token) -> bool

Attempts to reserve a slot for sending a message.

unsafe fn write(&self, token: &mut Token, msg: T) -> Result<(), T>

Writes a message into the channel.

fn start_recv(&self, token: &mut Token) -> bool

Attempts to reserve a slot for receiving a message.

unsafe fn read(&self, token: &mut Token) -> Result<T, ()>

Reads a message from the channel.

fn try_send(&self, msg: T) -> Result<(), TrySendError<T>>

Attempts to send a message into the channel.

fn send(&self, msg: T, deadline: Option<Instant>) -> Result<(), SendTimeoutError<T>>

Sends a message into the channel.

fn try_recv(&self) -> Result<T, TryRecvError>

Attempts to receive a message without blocking.

fn recv(&self, deadline: Option<Instant>) -> Result<T, RecvTimeoutError>

Receives a message from the channel.

fn len(&self) -> usize

Returns the current number of messages inside the channel.

fn capacity(&self) -> Option<usize>

Returns the capacity of the channel.

fn disconnect_senders(&self) -> bool

Disconnects senders and wakes up all blocked receivers.

Returns true if this call disconnected the channel.

unsafe fn disconnect_receivers(&self) -> bool

Disconnects receivers and wakes up all blocked senders.

Returns true if this call disconnected the channel.

Safety

May only be called once upon dropping the last receiver. The destruction of all other receivers must have been observed with acquire ordering or stronger.

unsafe fn discard_all_messages(&self, tail: usize)

Discards all messages.

tail should be the current (and therefore last) value of tail.

Panicking

If a destructor panics, the remaining messages are leaked, matching the behavior of the unbounded channel.

Safety

This method must only be called when dropping the last receiver. The destruction of all other receivers must have been observed with acquire ordering or stronger.

fn is_disconnected(&self) -> bool

Returns true if the channel is disconnected.

fn is_empty(&self) -> bool

Returns true if the channel is empty.

fn is_full(&self) -> bool

Returns true if the channel is full.

Auto Trait Implementations

impl<T> !Freeze for Channel<T>

impl<T> !RefUnwindSafe for Channel<T>

impl<T> !Send for Channel<T>

impl<T> !Sync for Channel<T>

impl<T> Unpin for Channel<T> where Box<[Slot<T>]>: Unpin,

impl<T> UnsafeUnpin for Channel<T> where Box<[Slot<T>]>: UnsafeUnpin,

impl<T> UnwindSafe for Channel<T> where Box<[Slot<T>]>: UnwindSafe,

Blanket Implementations

impl<T> Any for Channel<T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

impl<T> From<T> for Channel<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> SizeHint for Channel<T> where T: ?Sized,

fn lower_bound(&self) -> usize
fn upper_bound(&self) -> Option<usize>

impl<T> SizedTypeProperties for Channel<T>

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

fn into(self) -> U

Calls U::from(self).

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

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

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

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

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