pub struct RingBuffer<T, const N: usize> { /* private fields */ }
Implementations§
Source§impl<T, const N: usize> RingBuffer<T, N>
impl<T, const N: usize> RingBuffer<T, N>
pub fn new() -> (RingBufferWriter<T, N>, RingBufferReader<T, N>)
👎Deprecated since 0.1.8: please use
init()
instead.Sourcepub fn init() -> (RingBufferWriter<T, N>, RingBufferReader<T, N>)
pub fn init() -> (RingBufferWriter<T, N>, RingBufferReader<T, N>)
Initialize the RingBuffer with the given capacity
Examples found in repository?
examples/throughput.rs (line 7)
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§
Auto Trait Implementations§
impl<T, const N: usize> !Freeze for RingBuffer<T, N>
impl<T, const N: usize> !RefUnwindSafe for RingBuffer<T, N>
impl<T, const N: usize> Send for RingBuffer<T, N>where
T: Send,
impl<T, const N: usize> !Sync for RingBuffer<T, N>
impl<T, const N: usize> Unpin for RingBuffer<T, N>where
T: Unpin,
impl<T, const N: usize> UnwindSafe for RingBuffer<T, N>where
T: UnwindSafe,
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