pub struct UnownedReadBuffer<const S: usize> { /* private fields */ }
Expand description
Unowned Read buffer.
§S Generic: Size of the buffer.
beware that if this size is too large, and you stack allocate this struct then you will hit the guard page and your program will crash.
Implementations§
Source§impl<const S: usize> UnownedReadBuffer<S>
impl<const S: usize> UnownedReadBuffer<S>
Source§impl<const S: usize> UnownedReadBuffer<S>
impl<const S: usize> UnownedReadBuffer<S>
Sourcepub const fn available(&self) -> usize
pub const fn available(&self) -> usize
returns the amount of bytes that can still be read from the internal buffer.
Sourcepub fn ensure_readable<T: Read>(&mut self, read: &mut T) -> Result<bool>
pub fn ensure_readable<T: Read>(&mut self, read: &mut T) -> Result<bool>
This fn will return true if at least one byte can be read.
If the internal buffer is not empty this fn immediately returns true.
If the internal buffer is empty then it will call read()
once and return true if the read did not return Ok(0).
§Errors
propagated from Read, including TimedOut
and WouldBlock
Sourcepub fn try_read(&mut self, buffer: &mut [u8]) -> usize
pub fn try_read(&mut self, buffer: &mut [u8]) -> usize
This fn reads as many bytes as possible from the internal buffer. it returns 0 if the internal buffer is empty.
Sourcepub fn read<T: Read>(
&mut self,
read: &mut T,
buffer: &mut [u8],
) -> Result<usize>
pub fn read<T: Read>( &mut self, read: &mut T, buffer: &mut [u8], ) -> Result<usize>
This fn will read as many bytes as possible from the internal buffer.
If the internal buffer is empty when this fn is called then 1 call to the Read
impl is made to fill the buffer.
This fn only returns Ok(0) if the 1 call to the underlying read impl returned 0.
This fn does not call the read impl if available()
!= 0.
§Errors
Propagated from the Read
impl
Sourcepub fn read_exact<T: Read>(
&mut self,
read: &mut T,
buffer: &mut [u8],
) -> Result<()>
pub fn read_exact<T: Read>( &mut self, read: &mut T, buffer: &mut [u8], ) -> Result<()>
This fn will read the entire buffer from either the internal buffer or the
Read
impl. Multiple calls to the read impl may be made if necessary to fill the buffer.
§Errors
Propagated from the Read
impl
ErrorKind::UnexpectedEof
if the Read
impl returns Ok(0) before the buffer was filled.
Sourcepub fn read_until<T: Read>(
&mut self,
read: &mut T,
byte: u8,
buf: &mut Vec<u8>,
) -> Result<usize>
pub fn read_until<T: Read>( &mut self, read: &mut T, byte: u8, buf: &mut Vec<u8>, ) -> Result<usize>
Reads until either EOF happens or the desired byte is found.
This fn may call the underlying Read
impl multiple times until the buffer is filled.
§Errors
Propagated from the Read
impl
Sourcepub fn read_until_limit<T: Read>(
&mut self,
read: &mut T,
byte: u8,
limit: usize,
buf: &mut Vec<u8>,
) -> Result<usize>
pub fn read_until_limit<T: Read>( &mut self, read: &mut T, byte: u8, limit: usize, buf: &mut Vec<u8>, ) -> Result<usize>
Reads until either EOF happens or the desired byte is found or limit bytes have been appended to buf. The actual read impl may supply more bytes than limit, the excess is stored in the internal buffer in this case. Returns the amount of bytes appended to the buf vec.
§Errors
Propagated from the Read
impl
Sourcepub fn read_to_end<T: Read>(
&mut self,
read: &mut T,
buf: &mut Vec<u8>,
) -> Result<usize>
pub fn read_to_end<T: Read>( &mut self, read: &mut T, buf: &mut Vec<u8>, ) -> Result<usize>
Reads all remaining bytes into the buffer.
Those bytes may be from the internal buffer and then from the underlying Read
impl.
§Errors
Propagated from the Read
impl
Sourcepub fn read_to_string<T: Read>(
&mut self,
read: &mut T,
buf: &mut String,
) -> Result<usize>
pub fn read_to_string<T: Read>( &mut self, read: &mut T, buf: &mut String, ) -> Result<usize>
Reads all remaining bytes into the String.
Those bytes may be from the internal buffer and then from the underlying Read
impl.
If the Read
or buffer contained non-valid utf-8 sequences then this fn returns an io::Error
with Kind InvalidData
.
No data is lost in this case as bytes read and already placed in the buf are, in the buf and all remaining bytes
starting with those that were not valid utf-8 are in the internal buffer and can be fetched with a call to try_read()
.
§Errors
Propagated from the Read
impl
ErrorKind::InvalidData
if invalid utf-8 is found.
Sourcepub fn read_line<T: Read>(
&mut self,
read: &mut T,
buf: &mut String,
) -> Result<usize>
pub fn read_line<T: Read>( &mut self, read: &mut T, buf: &mut String, ) -> Result<usize>
Reads all bytes into the string until \n is found, or EOF occurred.
Data is first taken from the internal buffer and then taken from the Read
impl.
§Major difference from BufRead
’s read_line
fn:
this fn guarantees that no data is discarded when invalid utf-8 is encountered.
buf may contain some valid bytes in this case but all invalid bytes are retained in the internal buffer
so that the next call to read()
can pick them up. call available()
so you know how many bytes to read!
BufRead
’s read_line
fn just discards the last chunk of invalid data.
§Errors
Propagated from the Read
impl
ErrorKind::InvalidData
if invalid utf-8 is found.
Sourcepub fn fill_buf<T: Read>(&mut self, read: &mut T) -> Result<&[u8]>
pub fn fill_buf<T: Read>(&mut self, read: &mut T) -> Result<&[u8]>
ReadBuf
’s fill buf equivalent. This will only pull data from the underlying read if the internal buffer is empty.
§Errors
Propagated from the Read
impl
Sourcepub fn consume(&mut self, amt: usize)
pub fn consume(&mut self, amt: usize)
ReadBuf
’s consume fn.
In general, it should be paired with calls to fill_buf
§Panics
This function will panic if amt is > available
Sourcepub fn borrow<'a, T: Read>(
&'a mut self,
read: &'a mut T,
) -> BorrowedReadBuffer<'a, T, S> ⓘ
pub fn borrow<'a, T: Read>( &'a mut self, read: &'a mut T, ) -> BorrowedReadBuffer<'a, T, S> ⓘ
Borrows this unowned buffer and associates it with Read
impl.
The returned BorrowedReadBuffer
is both dyn Read
and dyn ReadBuf
.
This may be necessary to call some api function from a library that expects such datatypes.
The returned BorrowedReadBuffer
is subject to the lifetime of both the read and self.