Struct fixed_buffer::FixedBuf
source · [−]pub struct FixedBuf<const SIZE: usize> { /* private fields */ }
Expand description
A fixed-length byte buffer. You can write bytes to it and then read them back.
It is not a circular buffer. Call shift
periodically to
move unread bytes to the front of the buffer.
Implementations
sourceimpl<const SIZE: usize> FixedBuf<SIZE>
impl<const SIZE: usize> FixedBuf<SIZE>
sourcepub const fn new() -> Self
pub const fn new() -> Self
Makes a new empty buffer with space for SIZE
bytes.
Be careful of stack overflows!
sourcepub fn into_inner(self) -> [u8; SIZE]
pub fn into_inner(self) -> [u8; SIZE]
Drops the struct and returns its internal array.
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of unread bytes in the buffer.
Example
let mut buf: FixedBuf<16> = FixedBuf::new();
assert_eq!(0, buf.len());
buf.write_bytes("abc");
assert_eq!(3, buf.len());
buf.try_read_exact(2).unwrap();
assert_eq!(1, buf.len());
buf.shift();
assert_eq!(1, buf.len());
buf.read_all();
assert_eq!(0, buf.len());
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if there are unread bytes in the buffer.
Example
let mut buf: FixedBuf<16> = FixedBuf::new();
assert!(buf.is_empty());
buf.write_bytes("abc").unwrap();
assert!(!buf.is_empty());
buf.read_all();
assert!(buf.is_empty());
sourcepub fn escape_ascii(&self) -> String
pub fn escape_ascii(&self) -> String
Copies all readable bytes to a string. Includes printable ASCII characters as-is. Converts non-printable characters to strings like “\n” and “\x19”.
Uses
core::ascii::escape_default
internally to escape each byte.
This function is useful for printing byte slices to logs and comparing byte slices in tests.
Example test:
use fixed_buffer::FixedBuf;
let mut buf: FixedBuf<8> = FixedBuf::new();
buf.write_bytes("abc");
buf.write_bytes("€");
assert_eq!("abc\\xe2\\x82\\xac", buf.escape_ascii());
sourcepub fn mem(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn mem(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Borrows the entire internal memory buffer. This is a low-level function.
sourcepub fn readable(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn readable(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Returns the slice of readable bytes in the buffer.
After processing some bytes from the front of the slice,
call read
to consume the bytes.
This is a low-level method.
You probably want to use
std::io::Read::read
or
tokio::io::AsyncReadExt::read
, implemented for FixedBuf
in
fixed_buffer_tokio::AsyncReadExt
.
sourcepub fn try_read_byte(&mut self) -> Option<u8>
pub fn try_read_byte(&mut self) -> Option<u8>
Reads a single byte from the buffer.
Returns None
if the buffer is empty.
sourcepub fn try_read_exact(&mut self, num_bytes: usize) -> Option<&[u8]>
pub fn try_read_exact(&mut self, num_bytes: usize) -> Option<&[u8]>
Reads bytes from the buffer.
Returns None
if the buffer does not contain num_bytes
bytes.
sourcepub fn read_all(&mut self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn read_all(&mut self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Reads all the bytes from the buffer.
The buffer becomes empty and subsequent writes can fill the whole buffer.
sourcepub fn read_and_copy_bytes(&mut self, dest: &mut [u8]) -> usize
pub fn read_and_copy_bytes(&mut self, dest: &mut [u8]) -> usize
Reads bytes from the buffer and copies them into dest
.
Returns the number of bytes copied.
Returns 0
when the buffer is empty or dest
is zero-length.
sourcepub fn read_and_copy_exact(&mut self, dest: &mut [u8]) -> Option<()>
pub fn read_and_copy_exact(&mut self, dest: &mut [u8]) -> Option<()>
Reads byte from the buffer and copies them into dest
, filling it,
and returns Some(())
.
Returns None
if the buffer does not contain enough bytes tp fill dest
.
Returns Some(())
if dest
is zero-length.
sourcepub fn copy_once_from(&mut self, reader: &mut impl Read) -> Result<usize, Error>
pub fn copy_once_from(&mut self, reader: &mut impl Read) -> Result<usize, Error>
Reads from reader
once and writes the data into the buffer.
Use shift
to make empty space usable for writing.
Errors
Returns InvalidData
if there is no empty space in the buffer.
sourcepub fn write_bytes(
&mut self,
data: impl AsRef<[u8]>
) -> Result<usize, NoWritableSpace>
pub fn write_bytes(
&mut self,
data: impl AsRef<[u8]>
) -> Result<usize, NoWritableSpace>
Tries to write data
into the buffer, after any unread bytes.
Returns Ok(num_written)
, which may be less than the length of data
.
Returns Ok(0)
only when data
is empty.
Use shift
to make empty space usable for writing.
Errors
Returns NoWritableSpace
when the buffer has no free space at the end.
Example
let mut buf: FixedBuf<3> = FixedBuf::new();
assert_eq!(2, buf.write_bytes("ab").unwrap());
assert_eq!(1, buf.write_bytes("cd").unwrap()); // Fills buffer, "d" not written.
assert_eq!("abc", buf.escape_ascii());
buf.write_bytes("d").unwrap_err(); // Error, buffer is full.
sourcepub fn writable(&mut self) -> &mut [u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn writable(&mut self) -> &mut [u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Returns the writable part of the buffer.
To use this, first modify bytes at the beginning of the slice.
Then call wrote(usize)
to commit those bytes into the buffer and make them available for reading.
Returns an empty slice when the end of the buffer is full.
Use shift
to make empty space usable for writing.
This is a low-level method.
You probably want to use
std::io::Write::write
or
tokio::io::AsyncWriteExt::write
,
implemented for FixedBuf
in
fixed_buffer_tokio::AsyncWriteExt
.
Example
let mut buf: FixedBuf<8> = FixedBuf::new();
buf.writable()[0] = 'a' as u8;
buf.writable()[1] = 'b' as u8;
buf.writable()[2] = 'c' as u8;
buf.wrote(3);
assert_eq!("abc", buf.escape_ascii());
sourcepub fn wrote(&mut self, num_bytes: usize)
pub fn wrote(&mut self, num_bytes: usize)
Commits bytes into the buffer.
Call this after writing to the front of the
writable
slice.
This is a low-level method.
See writable()
.
Panics
Panics when there is not num_bytes
free at the end of the buffer.
sourcepub fn shift(&mut self)
pub fn shift(&mut self)
Recovers buffer space.
The buffer is not circular. After you read bytes, the space at the beginning of the buffer is unused. Call this method to move unread data to the beginning of the buffer and recover the space. This makes the free space available for writes, which go at the end of the buffer.
sourcepub fn deframe<F>(
&mut self,
deframer_fn: F
) -> Result<Option<Range<usize>>, Error> where
F: Fn(&[u8]) -> Result<(usize, Option<Range<usize>>), MalformedInputError>,
pub fn deframe<F>(
&mut self,
deframer_fn: F
) -> Result<Option<Range<usize>>, Error> where
F: Fn(&[u8]) -> Result<(usize, Option<Range<usize>>), MalformedInputError>,
This is a low-level function.
Use read_frame
instead.
Calls deframer_fn
to check if the buffer contains a complete frame.
Consumes the frame bytes from the buffer
and returns the range of the frame’s contents in the internal memory.
Use mem
to immutably borrow the internal memory and
construct the slice with &buf.mem()[range]
.
This is necessary because deframe
borrows self
mutably but
read_frame
needs to borrow it immutably and return a slice.
Returns None
if the buffer is empty or contains an incomplete frame.
Errors
Returns InvalidData
when deframer_fn
returns an error.
sourcepub fn read_frame<R, F>(
&mut self,
reader: &mut R,
deframer_fn: F
) -> Result<Option<&[u8]>, Error> where
R: Read,
F: Fn(&[u8]) -> Result<(usize, Option<Range<usize>>), MalformedInputError>,
pub fn read_frame<R, F>(
&mut self,
reader: &mut R,
deframer_fn: F
) -> Result<Option<&[u8]>, Error> where
R: Read,
F: Fn(&[u8]) -> Result<(usize, Option<Range<usize>>), MalformedInputError>,
Reads from reader
into the buffer.
After each read, calls deframer_fn
to check if the buffer now contains a complete frame.
Consumes the frame bytes from the buffer
and returns a slice with the frame contents.
Returns None
when reader
reaches EOF and the buffer is empty.
Calls shift
before reading.
Provided deframer functions:
Errors
Returns UnexpectedEof
when reader
reaches EOF and the buffer contains an incomplete frame.
Returns InvalidData
when deframer_fn
returns an error or the buffer fills up.
Example
let mut buf: FixedBuf<32> = FixedBuf::new();
let mut input = std::io::Cursor::new(b"aaa\r\nbbb\n\nccc\n");
loop {
if let Some(line) = buf.read_frame(&mut input, deframe_line).unwrap() {
println!("{}", escape_ascii(line));
} else {
// EOF.
break;
}
}
// Prints:
// aaa
// bbb
//
// ccc
Deframer Function deframe_fn
Checks if data
contains an entire frame.
Never panics.
Returns Ok((frame_len, Some(payload_range))
when data
contains a complete frame at &data[payload_range]
.
The caller should consume frame_len
from the beginning of the buffer
before calling deframe
again.
Returns Ok((frame_len, None))
if data
contains an incomplete frame.
The caller should consume frame_len
from the beginning of the buffer.
The caller can read more bytes and call deframe
again.
Returns Err(MalformedInputError)
if data
contains a malformed frame.
Popular frame formats:
- Newline-delimited: CSV, JSONL, HTTP, Server-Sent Events
text/event-stream
, and SMTP - Hexadecimal length prefix: HTTP chunked transfer encoding
- Binary length prefix: TLS
Example
use fixed_buffer::deframe_crlf;
assert_eq!(Ok((0, None)), deframe_crlf(b""));
assert_eq!(Ok((0, None)), deframe_crlf(b"abc"));
assert_eq!(Ok((0, None)), deframe_crlf(b"abc\r"));
assert_eq!(Ok((0, None)), deframe_crlf(b"abc\n"));
assert_eq!(Ok((5, Some((0..3)))), deframe_crlf(b"abc\r\n"));
assert_eq!(Ok((5, Some((0..3)))), deframe_crlf(b"abc\r\nX"));
Trait Implementations
sourceimpl<const SIZE: usize> Clone for FixedBuf<SIZE>
impl<const SIZE: usize> Clone for FixedBuf<SIZE>
sourcefn clone(&self) -> FixedBuf<SIZE>ⓘNotable traits for FixedBuf<SIZE>impl<const SIZE: usize> Write for FixedBuf<SIZE>impl<const SIZE: usize> Read for FixedBuf<SIZE>
fn clone(&self) -> FixedBuf<SIZE>ⓘNotable traits for FixedBuf<SIZE>impl<const SIZE: usize> Write for FixedBuf<SIZE>impl<const SIZE: usize> Read for FixedBuf<SIZE>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<const SIZE: usize> Ord for FixedBuf<SIZE>
impl<const SIZE: usize> Ord for FixedBuf<SIZE>
sourceimpl<const SIZE: usize> PartialOrd<FixedBuf<SIZE>> for FixedBuf<SIZE>
impl<const SIZE: usize> PartialOrd<FixedBuf<SIZE>> for FixedBuf<SIZE>
sourcefn partial_cmp(&self, other: &FixedBuf<SIZE>) -> Option<Ordering>
fn partial_cmp(&self, other: &FixedBuf<SIZE>) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl<const SIZE: usize> Read for FixedBuf<SIZE>
impl<const SIZE: usize> Read for FixedBuf<SIZE>
sourcefn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · sourcefn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like read
, except that it reads into a slice of buffers. Read more
sourcefn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)Determines if this Read
er has an efficient read_vectored
implementation. Read more
1.0.0 · sourcefn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
1.0.0 · sourcefn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
1.6.0 · sourcefn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
sourcefn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Pull some bytes from this source into the specified buffer. Read more
sourcefn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Read the exact number of bytes required to fill buf
. Read more
1.0.0 · sourcefn by_ref(&mut self) -> &mut Self
fn by_ref(&mut self) -> &mut Self
Creates a “by reference” adaptor for this instance of Read
. Read more
sourceimpl<const SIZE: usize> Write for FixedBuf<SIZE>
impl<const SIZE: usize> Write for FixedBuf<SIZE>
sourcefn write(&mut self, data: &[u8]) -> Result<usize, Error>
fn write(&mut self, data: &[u8]) -> Result<usize, Error>
Write a buffer into this writer, returning how many bytes were written. Read more
sourcefn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Flush this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
sourcefn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector
)Determines if this Write
r has an efficient write_vectored
implementation. Read more
1.0.0 · sourcefn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Attempts to write an entire buffer into this writer. Read more
sourcefn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored
)Attempts to write multiple buffers into this writer. Read more
impl<const SIZE: usize> Copy for FixedBuf<SIZE>
impl<const SIZE: usize> Eq for FixedBuf<SIZE>
impl<const SIZE: usize> StructuralEq for FixedBuf<SIZE>
impl<const SIZE: usize> StructuralPartialEq for FixedBuf<SIZE>
impl<const SIZE: usize> UnwindSafe for FixedBuf<SIZE>
Auto Trait Implementations
impl<const SIZE: usize> RefUnwindSafe for FixedBuf<SIZE>
impl<const SIZE: usize> Send for FixedBuf<SIZE>
impl<const SIZE: usize> Sync for FixedBuf<SIZE>
impl<const SIZE: usize> Unpin for FixedBuf<SIZE>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more