[−][src]Struct fixed_buffer_tokio::AsyncFixedBuf
A newtime that wraps
FixedBuf
and implements
tokio::io::AsyncRead
and
tokio::io::AsyncWrite
.
It also has async versions of FixedBuf's io functions.
Implementations
impl<T> AsyncFixedBuf<T>
[src]
pub const fn new(mem: T) -> Self
[src]
Creates a new FixedBuf and wraps it in an AsyncFixedBuf.
See
FixedBuf::new
for details.
pub fn into_inner(self) -> FixedBuf<T>
[src]
Drops the struct and returns its internal
FixedBuf
.
impl<T: AsRef<[u8]>> AsyncFixedBuf<T>
[src]
pub fn filled(mem: T) -> Self
[src]
Creates a new FixedBuf and wraps it in an AsyncFixedBuf.
Reading the buffer will return the bytes in mem
.
See
FixedBuf::filled
for details.
impl<T: AsMut<[u8]>> AsyncFixedBuf<T>
[src]
pub async fn copy_once_from<R: AsyncRead + Unpin + Send, '_, '_>(
&'_ mut self,
reader: &'_ mut R
) -> Result<usize, Error>
[src]
&'_ mut self,
reader: &'_ mut R
) -> Result<usize, Error>
Reads from reader
once and writes the data into the buffer.
Returns InvalidData
if there is no empty space in the buffer.
See shift
.
impl<T: AsRef<[u8]> + AsMut<[u8]>> AsyncFixedBuf<T>
[src]
pub async fn read_frame<R, F, '_, '_, '_>(
&'_ mut self,
reader: &'_ mut R,
deframer_fn: F
) -> Result<Option<&'_ [u8]>, Error> where
R: AsyncRead + Unpin + Send,
F: Fn(&[u8]) -> Result<Option<(Range<usize>, usize)>, MalformedInputError>,
[src]
&'_ mut self,
reader: &'_ mut R,
deframer_fn: F
) -> Result<Option<&'_ [u8]>, Error> where
R: AsyncRead + Unpin + Send,
F: Fn(&[u8]) -> Result<Option<(Range<usize>, usize)>, MalformedInputError>,
Async version of
FixedBuf::read_frame
.
Methods from Deref<Target = FixedBuf<T>>
pub fn len(&self) -> usize
[src]
Returns the number of unread bytes in the buffer.
Example:
let mut buf: FixedBuf<[u8; 16]> = FixedBuf::new([0u8; 16]); assert_eq!(0, buf.len()); buf.write_str("abc"); assert_eq!(3, buf.len()); buf.read_bytes(2); assert_eq!(1, buf.len()); buf.shift(); assert_eq!(1, buf.len()); buf.read_all(); assert_eq!(0, buf.len());
pub fn is_empty(&self) -> bool
[src]
Returns true if there are unread bytes in the buffer.
Example:
let mut buf: FixedBuf<[u8; 16]> = FixedBuf::new([0u8; 16]); assert!(buf.is_empty()); buf.write_str("abc").unwrap(); assert!(!buf.is_empty()); buf.read_all(); assert!(buf.is_empty());
pub fn clear(&mut self)
[src]
Discards all data in the buffer.
pub fn capacity(&self) -> usize
[src]
Returns the maximum number of bytes that can be stored in the buffer.
Example:
let mut buf: FixedBuf<[u8; 16]> = FixedBuf::new([0u8; 16]); assert_eq!(16, buf.capacity()); buf.write_str("abc").unwrap(); assert_eq!(16, buf.capacity());
pub fn escape_ascii(&self) -> String
[src]
Copies all readable bytes to a string. Includes printable ASCII characters as-is. Converts non-printable characters to strings like "\n" and "\x19".
Leaves the buffer unchanged.
Uses
core::ascii::escape_default
(https://doc.rust-lang.org/core/ascii/fn.escape_default.html
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::new([0u8; 16]); buf.write_str("abc"); buf.write_str("€"); assert_eq!("abc\\xe2\\x82\\xac", buf.escape_ascii());
pub fn mem(&self) -> &[u8]
[src]
This is a low-level function.
Borrows the entire internal memory buffer.
pub fn readable(&self) -> &[u8]
[src]
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 FixedBuffer in
fixed_buffer_tokio::AsyncReadExt
.
pub fn read_bytes(&mut self, num_bytes: usize) -> &[u8]
[src]
Reads bytes from the buffer.
Panics if the buffer does not contain enough bytes.
pub fn read_all(&mut self) -> &[u8]
[src]
Reads all the bytes from the buffer.
The buffer becomes empty and subsequent writes can fill the whole buffer.
pub fn read_and_copy_bytes(&mut self, dest: &mut [u8]) -> usize
[src]
Reads byte 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.
pub fn copy_once_from<R>(&mut self, reader: &mut R) -> Result<usize, Error> where
R: Read,
[src]
R: Read,
Reads from reader
once and writes the data into the buffer.
Returns InvalidData
if there is no empty space in the buffer.
See shift
.
pub fn write_str(&mut self, s: &str) -> Result<(), NotEnoughSpaceError>
[src]
Writes s
into the buffer, after any unread bytes.
Returns Err
if the buffer doesn't have enough free space at the end
for the whole string.
See shift
.
Example:
let mut buf: FixedBuf<[u8; 8]> = FixedBuf::new([0u8; 8]); buf.write_str("123").unwrap(); buf.write_str("456").unwrap(); assert_eq!("1234", escape_ascii(buf.read_bytes(4))); buf.write_str("78").unwrap(); buf.write_str("9").unwrap_err(); // End of buffer is full.
pub fn write_bytes(&mut self, data: &[u8]) -> Result<usize, NotEnoughSpaceError>
[src]
Tries to write data
into the buffer, after any unread bytes.
Returns Ok(data.len())
if it wrote all of the bytes.
Returns NotEnoughSpaceError
if the buffer doesn't have enough free space at the end for all of the bytes.
See shift
.
Example:
let mut buf: FixedBuf<[u8; 8]> = FixedBuf::new([0u8; 8]); assert_eq!(3 as usize, buf.write_bytes("123".as_bytes()).unwrap()); assert_eq!(3 as usize, buf.write_bytes("456".as_bytes()).unwrap()); assert_eq!("1234", escape_ascii(buf.read_bytes(4))); assert_eq!(2 as usize, buf.write_bytes("78".as_bytes()).unwrap()); // Fills buffer. buf.write_bytes("9".as_bytes()).unwrap_err(); // Error, buffer is full.
pub fn writable(&mut self) -> Option<&mut [u8]>
[src]
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 None
when the end of the buffer is full.
See shift
.
This is a low-level method.
You probably want to use
std::io::Write::write
or
tokio::io::AsyncWriteExt::write
,
implemented for FixedBuffer in
fixed_buffer_tokio::AsyncWriteExt
.
Example:
let mut buf: FixedBuf<[u8; 8]> = FixedBuf::new([0u8; 8]); buf.writable().unwrap()[0] = 'a' as u8; buf.writable().unwrap()[1] = 'b' as u8; buf.writable().unwrap()[2] = 'c' as u8; buf.wrote(3); assert_eq!("abc", escape_ascii(buf.read_bytes(3)));
pub fn wrote(&mut self, num_bytes: usize)
[src]
Commits bytes into the buffer.
Call this after writing to the front of the
writable
slice.
This is a low-level method.
Panics when there is not num_bytes
free at the end of the buffer.
See writable()
.
pub fn shift(&mut self)
[src]
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.
pub fn deframe<F>(
&mut self,
deframer_fn: F
) -> Result<Option<Range<usize>>, Error> where
F: Fn(&[u8]) -> Result<Option<(Range<usize>, usize)>, MalformedInputError>,
[src]
&mut self,
deframer_fn: F
) -> Result<Option<Range<usize>>, Error> where
F: Fn(&[u8]) -> Result<Option<(Range<usize>, 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 &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.
Returns InvalidData
when deframer_fn
returns an error.
pub fn read_frame<R, F>(
&mut self,
reader: &mut R,
deframer_fn: F
) -> Result<Option<&[u8]>, Error> where
F: Fn(&[u8]) -> Result<Option<(Range<usize>, usize)>, MalformedInputError>,
R: Read,
[src]
&mut self,
reader: &mut R,
deframer_fn: F
) -> Result<Option<&[u8]>, Error> where
F: Fn(&[u8]) -> Result<Option<(Range<usize>, usize)>, MalformedInputError>,
R: Read,
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.
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.
Calls shift
before reading.
Provided deframer functions:
Example
let mut buf: FixedBuf<[u8; 32]> = FixedBuf::new([0u8; 32]); let mut input = std::io::Cursor::new(b"aaa\r\nbbb\n\nccc\n"); assert_eq!("aaa", escape_ascii(buf.read_frame(&mut input, deframe_line).unwrap().unwrap())); assert_eq!("bbb", escape_ascii(buf.read_frame(&mut input, deframe_line).unwrap().unwrap())); assert_eq!("", escape_ascii(buf.read_frame(&mut input, deframe_line).unwrap().unwrap())); assert_eq!("ccc", escape_ascii(buf.read_frame(&mut input, deframe_line).unwrap().unwrap())); assert_eq!(None, buf.read_frame(&mut input, deframe_line).unwrap());
Deframer Function deframe_fn
Checks if data
contains an entire frame.
Never panics.
Returns Err(MalformedInputError)
if data
contains a malformed frame.
Returns Ok(None)
if data
contains an incomplete frame.
The caller will read more data and call deframe
again.
Returns Ok((range, len))
when data
contains a complete well-formed
frame of length len
and contents &data[range]
.
A frame is a sequence of bytes containing a payload and metadata prefix or suffix bytes. The metadata define where the payload starts and ends. Popular frame protocols include line-delimiting (CSV), hexadecimal length prefix (HTTP chunked transfer encoding), and binary length prefix (TLS).
If the caller removes len
bytes from the front of data
, then
data
should start with the next frame, ready to call deframe
again.
A line-delimited deframer returns range
so that &data[range]
contains the entire line without the
final b'\n'
or b"\r\n"
.
It returns len
that counts the bytes of
the entire line and the final b'\n'
or b"\r\n"
.
Example
A CRLF-terminated line deframer:
assert_eq!(Ok(None), deframe_crlf(b"")); assert_eq!(Ok(None), deframe_crlf(b"abc")); assert_eq!(Ok(None), deframe_crlf(b"abc\r")); assert_eq!(Ok(None), deframe_crlf(b"abc\n")); assert_eq!(Ok(Some((0..3, 5))), deframe_crlf(b"abc\r\n")); assert_eq!(Ok(Some((0..3, 5))), deframe_crlf(b"abc\r\nX"));
Trait Implementations
impl<T: AsRef<[u8]> + Unpin> AsyncRead for AsyncFixedBuf<T>
[src]
pub fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &mut ReadBuf<'_>
) -> Poll<Result<(), Error>>
impl<T: AsMut<[u8]>> AsyncWrite for AsyncFixedBuf<T>
[src]
pub fn poll_write(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
pub fn poll_flush(
self: Pin<&mut Self>,
_cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut Self>,
_cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
pub fn poll_shutdown(
self: Pin<&mut Self>,
_cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
[src]
self: Pin<&mut Self>,
_cx: &mut Context<'_>
) -> Poll<Result<(), Error>>
pub fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
[src]
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
pub fn is_write_vectored(&self) -> bool
[src]
impl<T> Deref for AsyncFixedBuf<T>
[src]
type Target = FixedBuf<T>
The resulting type after dereferencing.
pub fn deref(&self) -> &Self::Target
[src]
impl<T> DerefMut for AsyncFixedBuf<T>
[src]
impl<T> Unpin for AsyncFixedBuf<T>
[src]
Auto Trait Implementations
impl<T> RefUnwindSafe for AsyncFixedBuf<T> where
T: RefUnwindSafe,
T: RefUnwindSafe,
impl<T> Send for AsyncFixedBuf<T> where
T: Send,
T: Send,
impl<T> Sync for AsyncFixedBuf<T> where
T: Sync,
T: Sync,
impl<T> UnwindSafe for AsyncFixedBuf<T> where
T: UnwindSafe,
T: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<R> AsyncReadExt for R where
R: AsyncRead + ?Sized,
[src]
R: AsyncRead + ?Sized,
pub fn chain<R>(self, next: R) -> Chain<Self, R> where
R: AsyncRead,
[src]
R: AsyncRead,
pub fn read(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_buf<B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B> where
B: BufMut,
Self: Unpin,
[src]
B: BufMut,
Self: Unpin,
pub fn read_exact(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_u8(&'a mut self) -> ReadU8<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_i8(&'a mut self) -> ReadI8<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_u16(&'a mut self) -> ReadU16<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_i16(&'a mut self) -> ReadI16<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_u32(&'a mut self) -> ReadU32<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_i32(&'a mut self) -> ReadI32<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_u64(&'a mut self) -> ReadU64<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_i64(&'a mut self) -> ReadI64<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_u128(&'a mut self) -> ReadU128<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_i128(&'a mut self) -> ReadI128<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_u16_le(&'a mut self) -> ReadU16Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_i16_le(&'a mut self) -> ReadI16Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_u32_le(&'a mut self) -> ReadU32Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_i32_le(&'a mut self) -> ReadI32Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_u64_le(&'a mut self) -> ReadU64Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_i64_le(&'a mut self) -> ReadI64Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_u128_le(&'a mut self) -> ReadU128Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_i128_le(&'a mut self) -> ReadI128Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn read_to_end(
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEnd<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
buf: &'a mut Vec<u8, Global>
) -> ReadToEnd<'a, Self> where
Self: Unpin,
pub fn read_to_string(
&'a mut self,
dst: &'a mut String
) -> ReadToString<'a, Self> where
Self: Unpin,
[src]
&'a mut self,
dst: &'a mut String
) -> ReadToString<'a, Self> where
Self: Unpin,
pub fn take(self, limit: u64) -> Take<Self>
[src]
impl<W> AsyncWriteExt for W where
W: AsyncWrite + ?Sized,
[src]
W: AsyncWrite + ?Sized,
pub fn write(&'a mut self, src: &'a [u8]) -> Write<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_buf<B>(&'a mut self, src: &'a mut B) -> WriteBuf<'a, Self, B> where
B: Buf,
Self: Unpin,
[src]
B: Buf,
Self: Unpin,
pub fn write_all(&'a mut self, src: &'a [u8]) -> WriteAll<'a, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_u8(&'a mut self, n: u8) -> WriteU8<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_i8(&'a mut self, n: i8) -> WriteI8<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_u16(&'a mut self, n: u16) -> WriteU16<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_i16(&'a mut self, n: i16) -> WriteI16<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_u32(&'a mut self, n: u32) -> WriteU32<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_i32(&'a mut self, n: i32) -> WriteI32<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_u64(&'a mut self, n: u64) -> WriteU64<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_i64(&'a mut self, n: i64) -> WriteI64<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_u128(&'a mut self, n: u128) -> WriteU128<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_i128(&'a mut self, n: i128) -> WriteI128<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_u16_le(&'a mut self, n: u16) -> WriteU16Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_i16_le(&'a mut self, n: i16) -> WriteI16Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_u32_le(&'a mut self, n: u32) -> WriteU32Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_i32_le(&'a mut self, n: i32) -> WriteI32Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_u64_le(&'a mut self, n: u64) -> WriteU64Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_i64_le(&'a mut self, n: i64) -> WriteI64Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_u128_le(&'a mut self, n: u128) -> WriteU128Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn write_i128_le(&'a mut self, n: i128) -> WriteI128Le<&'a mut Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn flush(&mut self) -> Flush<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
pub fn shutdown(&mut self) -> Shutdown<'_, Self> where
Self: Unpin,
[src]
Self: Unpin,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,