UnownedReadBuffer

Struct UnownedReadBuffer 

Source
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>

Source

pub const fn new() -> Self

Construct a new Buffer

§Panics

if S is smaller than 16

Source§

impl<const S: usize> UnownedReadBuffer<S>

Source

pub const fn available(&self) -> usize

returns the amount of bytes that can still be read from the internal buffer.

Source

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

Source

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.

Source

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

Source

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.

Source

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

Source

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

Source

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

Source

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.

Source

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.

Source

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

Source

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

Source

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.

Trait Implementations§

Source§

impl<const S: usize> Debug for UnownedReadBuffer<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for UnownedReadBuffer<0x4000>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<const S: usize> Freeze for UnownedReadBuffer<S>

§

impl<const S: usize> RefUnwindSafe for UnownedReadBuffer<S>

§

impl<const S: usize> Send for UnownedReadBuffer<S>

§

impl<const S: usize> Sync for UnownedReadBuffer<S>

§

impl<const S: usize> Unpin for UnownedReadBuffer<S>

§

impl<const S: usize> UnwindSafe for UnownedReadBuffer<S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.