Module asyncio

Source
Available on crate feature asyncio only.
Expand description

Async reader/writer queue for generic items or byte arrays.

Each queue has a Reader and Writer part. Data can be copied into the writer’s buffer and sent to the reader without locks or allocation, allowing nonblocking communication across threads.

Reading and writing with the queue does not require any allocation, with the downside that the queue has a fixed capacity on creation.

Unlike nonblocking, this queue allows asynchronously waiting for data on the reader end, or waiting for space on the writer end. For u8 storage, this means the queue can be used as a futures::AsyncRead and futures::AsyncWrite if the std-io feature is enabled.

If you are not using an async runtime, you are probably more interested in the blocking queue, which blocks instead of waiting asynchronously.

§Example

use futures::join;
use futures::executor::block_on;
use mini_io_queue::asyncio::queue;

let (mut reader, mut writer) = queue(8);

let write_loop = async {
    for i in 0..16 {
        writer.write(&[i]).await;
    }
};

let read_loop = async {
    for i in 0..16 {
        let mut buf = [0];
        reader.read_exact(&mut buf).await.unwrap();

        assert_eq!(buf[0], i);
    }
};

block_on(async { join!(write_loop, read_loop) });

Structs§

Reader
Receives items from the queue.
Writer
Adds items to the queue.

Enums§

ReadExactError
An error indicating why reading failed.
WriteError
An error indicating why a writer failed.

Functions§

queueheap-buffer
Creates a queue with a specific capacity, allocating storage on the heap. The queue will be initialized with an empty read buffer and a full write buffer containing the element’s default value.
queue_from
Creates a queue that is backed by a specific storage. The queue will use the storage’s entire capacity, and will be initialized with an empty read buffer and a full write buffer.
queue_from_parts
Creates a queue from a separately allocated ring and storage. The queue will use the ring’s capacity, and be initialized with a read buffer from the ring’s left region and a write buffer from the ring’s right region.