Available on crate feature
blocking
only.Expand description
Synchronous 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 allocations, 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 blocks to wait for data on the reader end, or wait for space
on the writer end. For u8
storage, this means the queue can be used as a Read
or
Write
.
If you are using an async runtime, you are probably more interested in the asyncio
queue,
which does not block.
§Example
use mini_io_queue::blocking::queue;
let (mut reader, mut writer) = queue(8);
let write_thread = std::thread::spawn(move || {
for i in 0..16 {
writer.write(&[i]);
}
});
let read_thread = std::thread::spawn(move || {
for i in 0..16 {
let mut buf = [0];
reader.read_exact(&mut buf).unwrap();
assert_eq!(buf[0], i);
}
});
write_thread.join().unwrap();
read_thread.join().unwrap();
Structs§
Enums§
- Read
Exact Error - An error indicating why reading failed.
- Write
Error - An error indicating why a writer failed.
Functions§
- queue
heap-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.