Consumer

Struct Consumer 

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

Consumer half of the ring buffer

环形缓冲区的消费者端

§Type Parameters

  • T: Element type
  • N: Stack capacity threshold

§类型参数

  • T: 元素类型
  • N: 栈容量阈值

Implementations§

Source§

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

Source

pub fn pop(&mut self) -> Result<T, PopError>

Pop a value from the buffer

从缓冲区弹出一个值

§Errors

Returns PopError::Empty if the buffer is empty

§错误

如果缓冲区空则返回 PopError::Empty

Source

pub fn is_empty(&self) -> bool

Check if the buffer is empty

检查缓冲区是否为空

Source

pub fn slots(&self) -> usize

Get the number of elements currently in the buffer

获取缓冲区中当前的元素数量

Source

pub fn len(&self) -> usize

Get the number of elements currently in the buffer (alias for slots)

获取缓冲区中当前的元素数量(slots 的别名)

Source

pub fn capacity(&self) -> usize

Get the capacity of the buffer

获取缓冲区容量

Source

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 执行可能修改缓冲区的其他操作时有效。

Source

pub fn clear(&mut self)

Clear all elements from the buffer

清空缓冲区中的所有元素

This method pops and drops all elements currently in the buffer.

此方法弹出并 drop 缓冲区中当前的所有元素。

Source

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());
Source

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>

Source

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.

§性能

此方法使用优化的内存拷贝操作,性能优于逐个弹出元素。

Trait Implementations§

Source§

impl<T: Debug, const N: usize> Debug for Consumer<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 Consumer<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 Consumer<T, N>

§

impl<T, const N: usize> RefUnwindSafe for Consumer<T, N>
where T: RefUnwindSafe,

§

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

§

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

§

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

§

impl<T, const N: usize> UnwindSafe for Consumer<T, N>
where T: RefUnwindSafe,

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.