pub struct Consumer<T, const N: usize> { /* private fields */ }Expand description
Implementations§
Source§impl<T, const N: usize> Consumer<T, N>
impl<T, const N: usize> Consumer<T, N>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of elements currently in the buffer (alias for slots)
获取缓冲区中当前的元素数量(slots 的别名)
Sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Peek at the first element without removing it
查看第一个元素但不移除它
§Returns
Some(&T) if there is an element, None if the buffer is empty
§返回值
如果有元素则返回 Some(&T),如果缓冲区为空则返回 None
§Safety
The returned reference is valid only as long as no other operations are performed on the Consumer that might modify the buffer.
§安全性
返回的引用仅在未对 Consumer 执行可能修改缓冲区的其他操作时有效。
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clear all elements from the buffer
清空缓冲区中的所有元素
This method pops and drops all elements currently in the buffer.
此方法弹出并 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 elements from the buffer. The iterator will continue until the buffer is empty.
返回一个从缓冲区中移除并返回元素的迭代器。 迭代器将持续运行直到缓冲区为空。
§Examples
use smallring::spsc::new;
use std::num::NonZero;
let (mut producer, mut consumer) = new::<i32, 32>(NonZero::new(8).unwrap());
producer.push(1).unwrap();
producer.push(2).unwrap();
producer.push(3).unwrap();
let items: Vec<i32> = consumer.drain().collect();
assert_eq!(items, vec![1, 2, 3]);
assert!(consumer.is_empty());Sourcepub fn buffer(&self) -> &SharedData<T, N>
pub fn buffer(&self) -> &SharedData<T, N>
Get a reference to the shared buffer data
获取共享缓冲区数据的引用
Source§impl<T: Copy, const N: usize> Consumer<T, N>
impl<T: Copy, const N: usize> Consumer<T, N>
Sourcepub fn pop_slice(&mut self, dest: &mut [T]) -> usize
pub fn pop_slice(&mut self, dest: &mut [T]) -> usize
Pop multiple values into a slice
将多个值批量弹出到切片
This method attempts to pop as many elements as possible into the provided slice. It returns the number of elements successfully popped.
此方法尝试将尽可能多的元素弹出到提供的切片中。 返回成功弹出的元素数量。
§Parameters
dest: Destination slice to pop values into
§Returns
Number of elements successfully popped (0 to dest.len())
§参数
dest: 用于接收值的目标切片
§返回值
成功弹出的元素数量(0 到 dest.len())
§Performance
This method uses optimized memory copy operations for better performance than popping elements one by one.
§性能
此方法使用优化的内存拷贝操作,性能优于逐个弹出元素。