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>
impl<T, const N: usize> Receiver<T, N>
Sourcepub async fn recv(&self) -> Option<T>
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
Sourcepub fn try_recv(&self) -> Result<T, TryRecvError>
pub fn try_recv(&self) -> Result<T, TryRecvError>
Sourcepub fn peek(&self) -> Option<&T>
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 执行可能修改缓冲区的其他操作时有效。
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all messages from the channel
清空通道中的所有消息
This method pops and drops all messages currently in the channel.
此方法弹出并 drop 通道中当前的所有消息。
Sourcepub fn drain(&mut self) -> Drain<'_, T, N> ⓘ
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>
impl<T: Copy, const N: usize> Receiver<T, N>
Sourcepub fn try_recv_slice(&mut self, dest: &mut [T]) -> usize
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())
Sourcepub async fn recv_slice(&mut self, dest: &mut [T]) -> usize
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