fixed-buffer
This is a Rust library with fixed-size buffers, useful for network protocol parsers.
Features
- Write bytes to the buffer and read them back
- Lives on the stack
- Does not allocate memory
- Supports tokio's AsyncRead and AsyncWrite
- Use it to read a stream, search for a delimiter, and save leftover bytes for the next read.
- Easy to learn & use. Easy to maintain code that uses it.
- Works with Rust
latest
,beta
, andnightly
- No macros
Limitations
- Not a circular buffer.
You can call
shift()
periodically to move unread bytes to the front of the buffer. - There is no
iterate_delimited(AsyncRead)
. Because of borrowing rules, such a function would need to return non-borrowed (allocated and copied) data.
Examples
Read and handle requests from a remote client:
use FixedBuf;
use Error;
use AsyncWriteExt;
use TcpStream;
async
For a runnable example, see examples/server.rs.
Read and process records:
async
Documentation
The filled
constructor is useful in tests.
Alternatives
- bytes
- buf_redux, circular buffer support
- std::io::BufReader
- std::io::BufWriter
- static-buffer, updated in 2016
- block-buffer, for processing fixed-length blocks of data
- arrayvec, vector with fixed capacity.
Release Process
- Edit
Cargo.toml
and bump version number. - Run
./release.sh
Changelog
- v0.1.6 - Add
filled(mem)
constructor. - v0.1.5 - Change
read_delimited
to returnOption<&[u8]>
, for clean EOF handling. - v0.1.4 - Add
clear()
. - v0.1.3
- Thanks to freax13 for these changes:
- Support any buffer size. Now you can make
FixedBuf<[u8; 42]>
. - Support any AsRef<[u8]> + AsMut<[u8]> value for internal memory:
[u8; N]
Box<[u8; N]>
&mut [u8]
Vec<u8>
- Support any buffer size. Now you can make
- Renamed
new_with_mem
tonew
. UseFixedBuf::default()
to construct anyFixedBuf<T: Default>
, which includes arrays of sizes up to 32.
- Thanks to freax13 for these changes:
- v0.1.2 - Updated documentation.
- v0.1.1 - First published version
TODO
- DONE - Try to make this crate comply with the Rust API Guidelines.
- DONE - Find out how to include Readme.md info in the crate's docs.
- DONE - Make the repo public
- DONE - Set up continuous integration tests and banner.
- DONE - Add some documentation tests
- DONE - Set up public repository on Gitlab.com
- DONE - Custom buffer length.
- DONE - Publish to creates.io
- DONE - Read through https://crate-ci.github.io/index.html
- DONE - Get a code review from an experienced rustacean
- DONE - Add and update a changelog
- Update it manually
- https://crate-ci.github.io/release/changelog.html
- Add features: std, tokio, async-std
- Simplify
read_delimited()
- Make a more generic read_frame that takes a frame detector function.
Make
read_delimited
use that. - Implement FixedBuf::chain(AsyncRead) which buffers reads like tokio::io::ReadBuf.
- Fix FixedBuf rustdoc link to box_benchmark.
- Switch to const generics once they are stable:
- Set up CI on:
- DONE - Linux x86 64-bit
- macOS
- Windows
- https://crate-ci.github.io/pr/testing.html#travisci
- Linux ARM 64-bit (Raspberry Pi 3 and newer)
- Linux ARM 32-bit (Raspberry Pi 2)
- RISCV & ESP32 firmware?