pub struct RingBufferWriter<T, const N: usize> { /* private fields */ }
Implementations§
Source§impl<T, const N: usize> RingBufferWriter<T, N>
impl<T, const N: usize> RingBufferWriter<T, N>
Sourcepub fn push(&mut self, t: T) -> Option<T>
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}
Trait Implementations§
impl<T: Send, const N: usize> Send for RingBufferWriter<T, N>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more