Struct PrefixedReadIo

Source
pub struct PrefixedReadIo<I, P> { /* private fields */ }
Expand description

PrefixedReadIO facilitates the addition of a prefix to an IO stream, enabling stream rewinding and peeking capabilities. Subsequent reads will preserve access to the original stream contents.


async fn demo<T>(mut stream: T)
where
    T: AsyncReadRent + AsyncWriteRent,
{
    // let stream = b"hello world";
    let buf = vec![0 as u8; 6];
    let (_, buf) = stream.read_exact(buf).await;
    assert_eq!(buf, b"hello ");

    let prefix_buf = std::io::Cursor::new(buf);
    let mut pio = PrefixedReadIo::new(stream, prefix_buf);

    let buf = vec![0 as u8; 11];
    let (_, buf) = pio.read_exact(buf).await;
    assert_eq!(buf, b"hello world");
}

Implementations§

Source§

impl<I, P> PrefixedReadIo<I, P>

Source

pub const fn new(io: I, prefix: P) -> Self

Create a PrefixedIo with given io and read prefix.

Source

pub const fn prefix_finished(&self) -> bool

If the prefix has read to eof

Source

pub fn into_inner(self) -> I

Into inner

Trait Implementations§

Source§

impl<I: AsyncReadRent, P: Read> AsyncReadRent for PrefixedReadIo<I, P>

Source§

async fn read<T: IoBufMut>(&mut self, buf: T) -> BufResult<usize, T>

Reads bytes from this source into the provided buffer, returning the number of bytes read. Read more
Source§

async fn readv<T: IoVecBufMut>(&mut self, buf: T) -> BufResult<usize, T>

Similar to read, but reads data into a slice of buffers. Read more
Source§

impl<I: AsyncWriteRent, P> AsyncWriteRent for PrefixedReadIo<I, P>

Source§

fn write<T: IoBuf>( &mut self, buf: T, ) -> impl Future<Output = BufResult<usize, T>>

Writes the contents of a buffer into this writer, returning the number of bytes written. Read more
Source§

fn writev<T: IoVecBuf>( &mut self, buf_vec: T, ) -> impl Future<Output = BufResult<usize, T>>

This function attempts to write the entire contents of buf_vec, but the write may not fully succeed, and it might also result in an error. The bytes will be written starting at the specified offset. Read more
Source§

fn flush(&mut self) -> impl Future<Output = Result<()>>

Flushes this output stream, ensuring that all buffered content is successfully written to its destination. Read more
Source§

fn shutdown(&mut self) -> impl Future<Output = Result<()>>

Shuts down the output stream, ensuring that the value can be cleanly dropped. Read more
Source§

impl<I: CancelableAsyncReadRent, P: Read> CancelableAsyncReadRent for PrefixedReadIo<I, P>

Source§

async fn cancelable_read<T: IoBufMut>( &mut self, buf: T, c: CancelHandle, ) -> BufResult<usize, T>

Same as read(2)
Source§

async fn cancelable_readv<T: IoVecBufMut>( &mut self, buf: T, c: CancelHandle, ) -> BufResult<usize, T>

Same as readv(2)
Source§

impl<I: CancelableAsyncWriteRent, P> CancelableAsyncWriteRent for PrefixedReadIo<I, P>

Source§

fn cancelable_write<T: IoBuf>( &mut self, buf: T, c: CancelHandle, ) -> impl Future<Output = BufResult<usize, T>>

Same as write(2)
Source§

fn cancelable_writev<T: IoVecBuf>( &mut self, buf_vec: T, c: CancelHandle, ) -> impl Future<Output = BufResult<usize, T>>

Same as writev(2)
Source§

fn cancelable_flush( &mut self, c: CancelHandle, ) -> impl Future<Output = Result<()>>

Flush buffered data if needed
Source§

fn cancelable_shutdown( &mut self, c: CancelHandle, ) -> impl Future<Output = Result<()>>

Same as shutdown
Source§

impl<I, P> Split for PrefixedReadIo<I, P>
where I: Split,

implement unsafe Split for PrefixedReadIo, it’s safe because read/write are independent, we can safely split them into two I/O parts.

Auto Trait Implementations§

§

impl<I, P> Freeze for PrefixedReadIo<I, P>
where I: Freeze, P: Freeze,

§

impl<I, P> RefUnwindSafe for PrefixedReadIo<I, P>

§

impl<I, P> Send for PrefixedReadIo<I, P>
where I: Send, P: Send,

§

impl<I, P> Sync for PrefixedReadIo<I, P>
where I: Sync, P: Sync,

§

impl<I, P> Unpin for PrefixedReadIo<I, P>
where I: Unpin, P: Unpin,

§

impl<I, P> UnwindSafe for PrefixedReadIo<I, P>
where I: UnwindSafe, P: UnwindSafe,

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<A> AsyncReadRentExt for A
where A: AsyncReadRent + ?Sized,

Source§

async fn read_exact<T>(&mut self, buf: T) -> (Result<usize, Error>, T)
where T: IoBufMut + 'static,

Read until buf capacity is fulfilled
Source§

async fn read_vectored_exact<T>(&mut self, buf: T) -> (Result<usize, Error>, T)
where T: IoVecBufMut + 'static,

Readv until buf capacity is fulfilled
Source§

async fn read_u8(&mut self) -> Result<u8, Error>

Read number in async way
Source§

async fn read_u16(&mut self) -> Result<u16, Error>

Read number in async way
Source§

async fn read_u32(&mut self) -> Result<u32, Error>

Read number in async way
Source§

async fn read_u64(&mut self) -> Result<u64, Error>

Read number in async way
Source§

async fn read_u128(&mut self) -> Result<u16, Error>

Read number in async way
Source§

async fn read_i8(&mut self) -> Result<i8, Error>

Read number in async way
Source§

async fn read_i16(&mut self) -> Result<i16, Error>

Read number in async way
Source§

async fn read_i32(&mut self) -> Result<i32, Error>

Read number in async way
Source§

async fn read_i64(&mut self) -> Result<i64, Error>

Read number in async way
Source§

async fn read_i128(&mut self) -> Result<i128, Error>

Read number in async way
Source§

async fn read_f32(&mut self) -> Result<f32, Error>

Read number in async way
Source§

async fn read_f64(&mut self) -> Result<f64, Error>

Read number in async way
Source§

async fn read_u8_le(&mut self) -> Result<u8, Error>

Read number in async way
Source§

async fn read_u16_le(&mut self) -> Result<u16, Error>

Read number in async way
Source§

async fn read_u32_le(&mut self) -> Result<u32, Error>

Read number in async way
Source§

async fn read_u64_le(&mut self) -> Result<u64, Error>

Read number in async way
Source§

async fn read_u128_le(&mut self) -> Result<u128, Error>

Read number in async way
Source§

async fn read_i8_le(&mut self) -> Result<i8, Error>

Read number in async way
Source§

async fn read_i16_le(&mut self) -> Result<i16, Error>

Read number in async way
Source§

async fn read_i32_le(&mut self) -> Result<i32, Error>

Read number in async way
Source§

async fn read_i64_le(&mut self) -> Result<i64, Error>

Read number in async way
Source§

async fn read_i128_le(&mut self) -> Result<i128, Error>

Read number in async way
Source§

async fn read_f32_le(&mut self) -> Result<f32, Error>

Read number in async way
Source§

async fn read_f64_le(&mut self) -> Result<f64, Error>

Read number in async way
Source§

impl<A> AsyncWriteRentExt for A
where A: AsyncWriteRent + ?Sized,

Source§

async fn write_all<T>(&mut self, buf: T) -> (Result<usize, Error>, T)
where T: IoBuf + 'static,

Write all
Source§

async fn write_vectored_all<T>(&mut self, buf: T) -> (Result<usize, Error>, T)
where T: IoVecBuf + 'static,

Write vectored all
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<A> CancelableAsyncReadRentExt for A

Source§

async fn cancelable_read_exact<T>( &mut self, buf: T, c: CancelHandle, ) -> (Result<usize, Error>, T)
where T: IoBufMut + 'static,

Read until buf capacity is fulfilled
Source§

async fn cancelable_read_vectored_exact<T>( &mut self, buf: T, c: CancelHandle, ) -> (Result<usize, Error>, T)
where T: IoVecBufMut + 'static,

Readv until buf capacity is fulfilled
Source§

async fn cancelable_read_u8(&mut self, c: CancelHandle) -> Result<u8, Error>

Read number in async way
Source§

async fn cancelable_read_u16(&mut self, c: CancelHandle) -> Result<u16, Error>

Read number in async way
Source§

async fn cancelable_read_u32(&mut self, c: CancelHandle) -> Result<u32, Error>

Read number in async way
Source§

async fn cancelable_read_u64(&mut self, c: CancelHandle) -> Result<u64, Error>

Read number in async way
Source§

async fn cancelable_read_u128(&mut self, c: CancelHandle) -> Result<u128, Error>

Read number in async way
Source§

async fn cancelable_read_i8(&mut self, c: CancelHandle) -> Result<i8, Error>

Read number in async way
Source§

async fn cancelable_read_i16(&mut self, c: CancelHandle) -> Result<i16, Error>

Read number in async way
Source§

async fn cancelable_read_i32(&mut self, c: CancelHandle) -> Result<i32, Error>

Read number in async way
Source§

async fn cancelable_read_i64(&mut self, c: CancelHandle) -> Result<i64, Error>

Read number in async way
Source§

async fn cancelable_read_i128(&mut self, c: CancelHandle) -> Result<i128, Error>

Read number in async way
Source§

async fn cancelable_read_f32(&mut self, c: CancelHandle) -> Result<f32, Error>

Read number in async way
Source§

async fn cancelable_read_f64(&mut self, c: CancelHandle) -> Result<f64, Error>

Read number in async way
Source§

async fn cancelable_read_u8_le(&mut self, c: CancelHandle) -> Result<u8, Error>

Read number in async way
Source§

async fn cancelable_read_u16_le( &mut self, c: CancelHandle, ) -> Result<u16, Error>

Read number in async way
Source§

async fn cancelable_read_u32_le( &mut self, c: CancelHandle, ) -> Result<u32, Error>

Read number in async way
Source§

async fn cancelable_read_u64_le( &mut self, c: CancelHandle, ) -> Result<u64, Error>

Read number in async way
Source§

async fn cancelable_read_u128_le( &mut self, c: CancelHandle, ) -> Result<u128, Error>

Read number in async way
Source§

async fn cancelable_read_i8_le(&mut self, c: CancelHandle) -> Result<i8, Error>

Read number in async way
Source§

async fn cancelable_read_i16_le( &mut self, c: CancelHandle, ) -> Result<i16, Error>

Read number in async way
Source§

async fn cancelable_read_i32_le( &mut self, c: CancelHandle, ) -> Result<i32, Error>

Read number in async way
Source§

async fn cancelable_read_i64_le( &mut self, c: CancelHandle, ) -> Result<i64, Error>

Read number in async way
Source§

async fn cancelable_read_i128_le( &mut self, c: CancelHandle, ) -> Result<i128, Error>

Read number in async way
Source§

async fn cancelable_read_f32_le( &mut self, c: CancelHandle, ) -> Result<f32, Error>

Read number in async way
Source§

async fn cancelable_read_f64_le( &mut self, c: CancelHandle, ) -> Result<f64, Error>

Read number in async way
Source§

impl<A> CancelableAsyncWriteRentExt for A

Source§

async fn write_all<T>( &mut self, buf: T, c: CancelHandle, ) -> (Result<usize, Error>, T)
where T: IoBuf + 'static,

Write all
Source§

async fn write_vectored_all<T>( &mut self, buf: T, c: CancelHandle, ) -> (Result<usize, Error>, T)
where T: IoVecBuf + 'static,

Write all
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> Splitable for T
where T: Split + AsyncWriteRent,

Source§

type OwnedRead = OwnedReadHalf<T>

Owned Read Split
Source§

type OwnedWrite = OwnedWriteHalf<T>

Owned Write Split
Source§

fn into_split( self, ) -> (<T as Splitable>::OwnedRead, <T as Splitable>::OwnedWrite)

Split into owned parts
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.