| Crates.io | fixed-buffer |
| lib.rs | fixed-buffer |
| version | 1.0.2 |
| created_at | 2020-10-27 05:18:36.551941+00 |
| updated_at | 2025-06-30 05:39:04.253896+00 |
| description | Fixed-size buffers for network protocol parsers |
| homepage | |
| repository | https://gitlab.com/leonhard-llc/fixed-buffer-rs |
| max_upload_size | |
| id | 305855 |
| size | 116,690 |
This is a Rust library with fixed-size buffers, useful for network protocol parsers and file parsers.
forbid(unsafe_code)stdasync-std-feature, futures-io, smol-feature, or tokio.shift() periodically
to move unread bytes to the front of the buffer.Read and handle requests from a remote client:
use fixed_buffer::{deframe_line, FixedBuf};
use std::io::Error;
use std::net::TcpStream;
fn handle_conn(mut tcp_stream: TcpStream) -> Result<(), Error> {
let mut buf: FixedBuf<4096> = FixedBuf::new();
loop {
// Read a line
// and leave leftover bytes in `buf`.
let line_bytes = match buf.read_frame(
&mut tcp_stream, deframe_line)? {
Some(line_bytes) => line_bytes,
None => return Ok(()),
};
let request = Request::parse(line_bytes)?;
handle_request(request)?;
}
}
For a complete example, see
tests/server.rs.
Read and process records:
use fixed_buffer::FixedBuf;
use std::io::{Error, ErrorKind, Read};
use std::net::TcpStream;
fn try_process_record(b: &[u8]) -> Result<usize, Error> {
if b.len() < 2 {
return Ok(0);
}
if b.starts_with("ab".as_bytes()) {
println!("found record");
Ok(2)
} else {
Err(Error::new(ErrorKind::InvalidData, "bad record"))
}
}
fn read_and_process<R: Read>(mut input: R) -> Result<(), Error> {
let mut buf: FixedBuf<1024> = FixedBuf::new();
loop {
// Read a chunk into the buffer.
if buf.copy_once_from(&mut input)? == 0 {
return if buf.len() == 0 {
// EOF at record boundary
Ok(())
} else {
// EOF in the middle of a record
Err(Error::from(
ErrorKind::UnexpectedEof))
};
}
// Process records in the buffer.
loop {
let num_to_consume =
try_process_record(buf.readable())?;
if num_to_consume == 0 {
break;
}
buf.try_read_exact(num_to_consume).unwrap();
}
// Shift data in the buffer to free up
// space at the end for writing.
buf.shift();
}
}
The From<&[u8; SIZE]> implementation is useful in tests. Example:
use core::convert::From;
assert_eq!(3, FixedBuf::from(b"abc").len());
Metric output format: x/y
x = unsafe code used by the build
y = total unsafe code found in the crate
Symbols:
🔒 = No `unsafe` usage found, declares #![forbid(unsafe_code)]
❓ = No `unsafe` usage found, missing #![forbid(unsafe_code)]
☢️ = `unsafe` usage found
Functions Expressions Impls Traits Methods Dependency
0/0 0/0 0/0 0/0 0/0 🔒 fixed-buffer 1.0.2
0/0 0/0 0/0 0/0 0/0
bytes, lots of unsafebuf_redux, circular buffer support, updated in 2019std::io::BufReaderstd::io::BufWriterstatic-buffer, updated in 2016block-buffer, for processing fixed-length blocks of data, some unsafearrayvec, vector with fixed capacity, some unsaferead_frame_tokio.From<&[u8; SIZE]>, FixedBuffer::from(b"x")ReadWriteChain and ReadWriteTake to new
read-write-ext crate.From<[u8; SIZE]>, FixedBuffer::from([0])write_bytes to take AsRef<[u8]>try_read_exact to read_and_copy_exact.try_read_bytes to try_read_exact.empty, filled, read_byte, read_bytes, try_parse, and write_str.deframe to allow consuming bytes without returning a framewrite_bytes to write as many bytes as it can,
and return new NoWritableSpace error only when it cannot write any bytes.
Remove NotEnoughSpaceError. The method now behaves like std::io::Write::write.From<NotEnoughSpaceError> and From<MalformedInputError> for String.FixedBuf<1024>.new arg.capacity.Copy impl.writable return type to &mut [u8].read_byte,
try_read_byte,
try_read_bytes,
try_read_exact,
try_parse.UnwindSafedeframe
and
mem,
needed by AsyncFixedBuf::read_frame.fixed_buffer_tokio.copy_once_from,
read_block,
ReadWriteChain,
and
ReadWriteTake.FixedBuf::escape_ascii.filled
constructor.read_delimited
to return Option<&[u8]>, for clean EOF handling.clear().FixedBuf<[u8; 42]>.AsRef<[u8]> + AsMut<[u8]> value for internal memory:
[u8; N]Box<[u8; N]>&mut [u8]Vec<u8>new_with_mem to new.
Use FixedBuf::default() to construct any FixedBuf<T: Default>, which includes
arrays of sizes up to 32.docs.rs.buf.slice(buf.read_frame(&mut reader, deframe_crlf))frame_copy_iter function.
Because of borrowing rules, this function must return non-borrowed (allocated and copied) data.Cargo.toml and bump version number.../release.shLicense: Apache-2.0