async-ringbuf 0.3.6

Async SPSC FIFO ring buffer
Documentation

async-ringbuf

Crates.io Docs.rs Github Actions Gitlab CI License

async/.await SPSC FIFO ring buffer.

Features

  • Arbitrary item type (not only Copy).
  • Items can be inserted and removed one by one or many at once.
  • Thread-safe direct access to the internal ring buffer memory.
  • Different types of buffers and underlying storages.
  • Can be used without std and even without alloc.
  • Does not depends on any async runtime (like tokio and async-std).

Overview

This technically is the original ringbuf with an async synchronization.

AsyncProducer and AsyncConsumer traits provide async/await analogs of lock-free methods of Producer and Consumer with the ability to wait for certain event (such as appearance item or free slot, completing transfer of all items, or closing the opposite endpoint).

Examples

Simple

use async_ringbuf::{traits::*, AsyncHeapRb};
use futures::{executor::block_on, join};

let rb = AsyncHeapRb::<i32>::new(10);
let (prod, cons) = rb.split();

const N_ITERS: i32 = 100;

join!(
    async move {
        let mut prod = prod;
        for i in 0..N_ITERS {
            prod.push(i).await.unwrap();
        }
    },
    async move {
        let mut cons = cons;
        for i in 0..N_ITERS {
            assert_eq!(cons.pop().await, Some(i));
        }
        assert_eq!(cons.pop().await, None);
    },
);

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.