Receiver

Struct Receiver 

Source
pub struct Receiver<T, const N: usize> { /* private fields */ }
Expand description

SPSC channel receiver

SPSC 通道接收器

Implementations§

Source§

impl<T, const N: usize> Receiver<T, N>

Source

pub async fn recv(&self) -> Option<T>

Receive a message from the channel (async, waits if buffer is empty)

Returns None if the channel is closed and empty

从通道接收消息(异步,如果缓冲区空则等待)

如果通道已关闭且为空,返回 None

Source

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

Try to receive a message without blocking

§Errors
  • Returns TryRecvError::Empty if the buffer is empty
  • Returns TryRecvError::Closed if the sender has been dropped and buffer is empty

尝试非阻塞地接收消息

§错误
  • 如果缓冲区空,返回 TryRecvError::Empty
  • 如果发送器已被丢弃且缓冲区空,返回 TryRecvError::Closed
Source

pub fn is_empty(&self) -> bool

Check if the channel is empty

检查通道是否为空

Source

pub fn len(&self) -> usize

Get the number of messages currently in the channel

获取通道中当前的消息数量

Source

pub fn capacity(&self) -> usize

Get the capacity of the channel

获取通道的容量

Source

pub fn peek(&self) -> Option<&T>

Peek at the first message without removing it

查看第一个消息但不移除它

§Returns

Some(&T) if there is a message, None if the channel is empty

§返回值

如果有消息则返回 Some(&T),如果通道为空则返回 None

§Safety

The returned reference is valid only as long as no other operations are performed on the Receiver that might modify the buffer.

§安全性

返回的引用仅在未对 Receiver 执行可能修改缓冲区的其他操作时有效。

Source

pub fn clear(&mut self)

Clear all messages from the channel

清空通道中的所有消息

This method pops and drops all messages currently in the channel.

此方法弹出并 drop 通道中当前的所有消息。

Source

pub fn drain(&mut self) -> Drain<'_, T, N>

Create a draining iterator

创建一个消费迭代器

Returns an iterator that removes and returns messages from the channel. The iterator will continue until the channel is empty.

返回一个从通道中移除并返回消息的迭代器。 迭代器将持续运行直到通道为空。

§Examples
use lite_sync::spsc::channel;
use std::num::NonZeroUsize;

    #[cfg(not(feature = "loom"))]
    {
        let (tx, mut rx) = channel::<i32, 8>(NonZeroUsize::new(32).unwrap());
        tx.try_send(1).unwrap();
        tx.try_send(2).unwrap();
        tx.try_send(3).unwrap();

        let items: Vec<i32> = rx.drain().collect();
        assert_eq!(items, vec![1, 2, 3]);
        assert!(rx.is_empty());
    }
Source§

impl<T: Copy, const N: usize> Receiver<T, N>

Source

pub fn try_recv_slice(&mut self, dest: &mut [T]) -> usize

Try to receive multiple values into a slice without blocking

尝试非阻塞地将多个值接收到切片

This method attempts to receive as many messages as possible into the provided slice. It returns the number of messages successfully received.

此方法尝试将尽可能多的消息接收到提供的切片中。 返回成功接收的消息数量。

§Parameters
  • dest: Destination slice to receive values into
§Returns

Number of messages successfully received (0 to dest.len())

§参数
  • dest: 用于接收值的目标切片
§返回值

成功接收的消息数量(0 到 dest.len())

Source

pub async fn recv_slice(&mut self, dest: &mut [T]) -> usize

Receive multiple values into a slice (async, waits if buffer is empty)

将多个值接收到切片(异步,如果缓冲区空则等待)

This method will fill the destination slice as much as possible, waiting if necessary when the buffer becomes empty. Returns the number of messages received.

此方法将尽可能填充目标切片,必要时在缓冲区空时等待。 返回接收的消息数量。

§Parameters
  • dest: Destination slice to receive values into
§Returns

Number of messages successfully received (0 to dest.len()) Returns 0 if the channel is closed and empty

§参数
  • dest: 用于接收值的目标切片
§返回值

成功接收的消息数量(0 到 dest.len()) 如果通道已关闭且为空,返回 0

Trait Implementations§

Source§

impl<T, const N: usize> Debug for Receiver<T, N>

Source§

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

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

impl<T, const N: usize> Drop for Receiver<T, N>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T, const N: usize> Freeze for Receiver<T, N>

§

impl<T, const N: usize> !RefUnwindSafe for Receiver<T, N>

§

impl<T, const N: usize> Send for Receiver<T, N>
where T: Send,

§

impl<T, const N: usize> Sync for Receiver<T, N>
where T: Send,

§

impl<T, const N: usize> Unpin for Receiver<T, N>

§

impl<T, const N: usize> !UnwindSafe for Receiver<T, N>

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> 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, 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.