Module nonblocking

Source
Available on crate feature nonblocking only.
Expand description

Non-blocking 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.

Reader and Writer can implement Read and Write if the std-io feature is enabled. These implementations will return WouldBlock errors instead of blocking.

§Example

use mini_io_queue::nonblocking::queue;

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

let write_thread = std::thread::spawn(move || {
    for i in 0..16 {
        let buf = [i];

        // spin until there is space to write
        loop {
            let write_len = writer.write(&buf);
            if write_len == 1 {
                break;
            }
        }
    }
});

let read_thread = std::thread::spawn(move || {
    for i in 0..16 {
        let mut buf = [0];

        // spin until there is data to read
        loop {
            let read_len = reader.read(&mut buf);
            if read_len == 1 {
                break;
            }
        }

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

write_thread.join().unwrap();
read_thread.join().unwrap();

Structs§

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

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.