| Crates.io | bytering |
| lib.rs | bytering |
| version | 0.7.0 |
| created_at | 2024-12-29 00:13:30.260225+00 |
| updated_at | 2025-04-07 21:50:44.469794+00 |
| description | A ring buffer specialized for vectored reading and writing in blocking and async I/O |
| homepage | |
| repository | https://github.com/cloneable/bytering |
| max_upload_size | |
| id | 1497844 |
| size | 37,151 |
bytering::BufferA simple, lock-free ring buffer for bytes with fixed capacity. It's specialized for low latency, vectored reading and writing in blocking and async I/O operations.
Similar to VecDeque it provides a pair of byte slices mapping the filled space
of its internal linear space. Unlike VecDeque it provides a pair of mutable
slices for writing into the empty space. These pairs of slices can be directly
passed to read_vectored (readv) and write_vectored (writev),
respectively.
If neither source nor target of data supports direct vectored I/O this ring buffer does not offer any advantages.
let (reader, writer) = bytering::new(4096, 4096);
let r = reader.io_slices(|bufs, _len| {
let r = input.read_vectored(bufs)?;
Ok(r)
})?;
let w = writer.io_slices(|bufs, _len| {
let w = output.write_vectored(bufs)?;
Ok(w)
})?;
The buffer is split into one reader half and one writer half after creation. Each half controls one atomic counter: either the read counter or the write counter. The counters are only ever incremented and the read counter cannot go past the write counter and the write counter cannot go further away from the read counter than the size of the buffer, ensuring that neither half accesses memory currently "held" by the other half.
The code contains some unsafe blocks:
alloc and dealloc functions.from_raw_parts(_mut).UnsafeCell.Clone nor Sync.