Struct RingBufferWriter

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

Implementations§

Source§

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

Source

pub fn push(&mut self, t: T) -> Option<T>

Push an element into the RingBuffer. It returns Some(T) if the RingBuffer is full, giving back the ownership of T.

Examples found in repository?
examples/throughput.rs (line 13)
6fn main() {
7    let (mut tx, mut rx) = RingBuffer::<usize, 16>::init();
8    let counter = Arc::new(AtomicUsize::new(0));
9
10    std::thread::spawn(move || {
11        let mut current: usize = 0;
12        loop {
13            if tx.push(current).is_none() {
14                current = current.wrapping_add(1);
15            } else {
16                std::thread::yield_now();
17            }
18        }
19    });
20
21    let c_counter = counter.clone();
22    std::thread::spawn(move || {
23        let mut current: usize = 0;
24        loop {
25            if let Some(c) = rx.pull() {
26                assert_eq!(c, current);
27                current = current.wrapping_add(1);
28                c_counter.fetch_add(1, Ordering::Relaxed);
29            } else {
30                std::thread::yield_now();
31            }
32        }
33    });
34
35    loop {
36        std::thread::sleep(Duration::from_secs(1));
37        println!("{} elem/s", counter.swap(0, Ordering::Relaxed));
38    }
39}
Source

pub fn is_full(&mut self) -> bool

Check if the RingBuffer is full and evenutally updates the internal cached indexes.

Trait Implementations§

Source§

impl<T: Send, const N: usize> Send for RingBufferWriter<T, N>

Source§

impl<T: Sync, const N: usize> Sync for RingBufferWriter<T, N>

Auto Trait Implementations§

§

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

§

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

§

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

§

impl<T, const N: usize> !UnwindSafe for RingBufferWriter<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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.