Struct LimitSeekerReader

Source
pub struct LimitSeekerReader<T>
where T: AsyncRead + AsyncSeek + Unpin,
{ /* private fields */ }
Expand description

An AsyncRead + AsyncSeek wrapper It supports limit the bytes can be read. Typically used when you want to read a specific segments from a file

Example

 
async fn run_test() -> Result<(), Box<dyn std::error::Error>> {
    use tokio::io::SeekFrom;
    use tokio::io::{AsyncRead,AsyncSeek, AsyncReadExt, AsyncSeekExt};
    let f = tokio::fs::File::open("input.txt").await?;
    let read_from: u64 = 18; // start read from 18'th byte
    let mut lsr = crate::asyncio_utils::LimitSeekerReader::new(f, Some(20)); // read up to 20 bytes
    lsr.seek(SeekFrom::Start(read_from)); // do seek
 
    let mut buf = vec![0u8; 1024];
    lsr.read(&mut buf); // read it
    return Ok(());
}

Implementations§

Source§

impl<T> LimitSeekerReader<T>
where T: AsyncRead + AsyncSeek + Unpin,

Implement a limit reader for AsyncRead and AsyncSeek together. Typically a file Note that if your seek does not affect total reads. You can seek with positive/negative from current/begin of file/end of file, but it does not change the total bytes would be read from the reader.

This is a little bit weird though. Typically what you want to do is just seek before reading.

This is useful when you want to service Http Get with Range requests.

You open tokio::fs::File you seek the position you set limit on number of bytes to read you start reading and serving.

Source

pub fn destruct(self) -> (usize, T)

Destruct the LimitSeekerReader and get the bytes read so far and the original reader Returns the size read and the original reader.

You can’t use the LimitSeekerReader after this call

Source

pub fn new(src: T, limit: Option<usize>) -> LimitSeekerReader<T>

Create new LimitSeekerReader from another AsyncRead + AsyncSeek (typically file)

Argument src is the underlying reader + seeker limit is the byte limit. Node that the limit can be Some(limit) None -> No limit (std::usize::MAX)

Trait Implementations§

Source§

impl<T> AsyncRead for LimitSeekerReader<T>
where T: AsyncRead + AsyncSeek + Unpin,

Implementation of AsyncRead

Source§

fn poll_read( self: Pin<&mut Self>, ctx: &mut Context<'_>, data: &mut ReadBuf<'_>, ) -> Poll<Result<(), Error>>

Attempts to read from the AsyncRead into buf. Read more
Source§

impl<T> AsyncSeek for LimitSeekerReader<T>
where T: AsyncRead + AsyncSeek + Unpin,

Implementation of AsyncSeek

Source§

fn start_seek(self: Pin<&mut Self>, from: SeekFrom) -> Result<(), Error>

Attempts to seek to an offset, in bytes, in a stream. Read more
Source§

fn poll_complete( self: Pin<&mut Self>, ctx: &mut Context<'_>, ) -> Poll<Result<u64, Error>>

Waits for a seek operation to complete. Read more

Auto Trait Implementations§

§

impl<T> Freeze for LimitSeekerReader<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for LimitSeekerReader<T>
where T: RefUnwindSafe,

§

impl<T> Send for LimitSeekerReader<T>
where T: Send,

§

impl<T> Sync for LimitSeekerReader<T>
where T: Sync,

§

impl<T> Unpin for LimitSeekerReader<T>

§

impl<T> UnwindSafe for LimitSeekerReader<T>
where T: 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<R> AsyncReadExt for R
where R: AsyncRead + ?Sized,

Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where Self: Sized, R: AsyncRead,

Creates a new AsyncRead instance that chains this stream with next. Read more
Source§

fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>
where Self: Unpin,

Pulls some bytes from this source into the specified buffer, returning how many bytes were read. Read more
Source§

fn read_buf<'a, B>(&'a mut self, buf: &'a mut B) -> ReadBuf<'a, Self, B>
where Self: Unpin, B: BufMut + ?Sized,

Pulls some bytes from this source into the specified buffer, advancing the buffer’s internal cursor. Read more
Source§

fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>
where Self: Unpin,

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_u8(&mut self) -> ReadU8<&mut Self>
where Self: Unpin,

Reads an unsigned 8 bit integer from the underlying reader. Read more
Source§

fn read_i8(&mut self) -> ReadI8<&mut Self>
where Self: Unpin,

Reads a signed 8 bit integer from the underlying reader. Read more
Source§

fn read_u16(&mut self) -> ReadU16<&mut Self>
where Self: Unpin,

Reads an unsigned 16-bit integer in big-endian order from the underlying reader. Read more
Source§

fn read_i16(&mut self) -> ReadI16<&mut Self>
where Self: Unpin,

Reads a signed 16-bit integer in big-endian order from the underlying reader. Read more
Source§

fn read_u32(&mut self) -> ReadU32<&mut Self>
where Self: Unpin,

Reads an unsigned 32-bit integer in big-endian order from the underlying reader. Read more
Source§

fn read_i32(&mut self) -> ReadI32<&mut Self>
where Self: Unpin,

Reads a signed 32-bit integer in big-endian order from the underlying reader. Read more
Source§

fn read_u64(&mut self) -> ReadU64<&mut Self>
where Self: Unpin,

Reads an unsigned 64-bit integer in big-endian order from the underlying reader. Read more
Source§

fn read_i64(&mut self) -> ReadI64<&mut Self>
where Self: Unpin,

Reads an signed 64-bit integer in big-endian order from the underlying reader. Read more
Source§

fn read_u128(&mut self) -> ReadU128<&mut Self>
where Self: Unpin,

Reads an unsigned 128-bit integer in big-endian order from the underlying reader. Read more
Source§

fn read_i128(&mut self) -> ReadI128<&mut Self>
where Self: Unpin,

Reads an signed 128-bit integer in big-endian order from the underlying reader. Read more
Source§

fn read_f32(&mut self) -> ReadF32<&mut Self>
where Self: Unpin,

Reads an 32-bit floating point type in big-endian order from the underlying reader. Read more
Source§

fn read_f64(&mut self) -> ReadF64<&mut Self>
where Self: Unpin,

Reads an 64-bit floating point type in big-endian order from the underlying reader. Read more
Source§

fn read_u16_le(&mut self) -> ReadU16Le<&mut Self>
where Self: Unpin,

Reads an unsigned 16-bit integer in little-endian order from the underlying reader. Read more
Source§

fn read_i16_le(&mut self) -> ReadI16Le<&mut Self>
where Self: Unpin,

Reads a signed 16-bit integer in little-endian order from the underlying reader. Read more
Source§

fn read_u32_le(&mut self) -> ReadU32Le<&mut Self>
where Self: Unpin,

Reads an unsigned 32-bit integer in little-endian order from the underlying reader. Read more
Source§

fn read_i32_le(&mut self) -> ReadI32Le<&mut Self>
where Self: Unpin,

Reads a signed 32-bit integer in little-endian order from the underlying reader. Read more
Source§

fn read_u64_le(&mut self) -> ReadU64Le<&mut Self>
where Self: Unpin,

Reads an unsigned 64-bit integer in little-endian order from the underlying reader. Read more
Source§

fn read_i64_le(&mut self) -> ReadI64Le<&mut Self>
where Self: Unpin,

Reads an signed 64-bit integer in little-endian order from the underlying reader. Read more
Source§

fn read_u128_le(&mut self) -> ReadU128Le<&mut Self>
where Self: Unpin,

Reads an unsigned 128-bit integer in little-endian order from the underlying reader. Read more
Source§

fn read_i128_le(&mut self) -> ReadI128Le<&mut Self>
where Self: Unpin,

Reads an signed 128-bit integer in little-endian order from the underlying reader. Read more
Source§

fn read_f32_le(&mut self) -> ReadF32Le<&mut Self>
where Self: Unpin,

Reads an 32-bit floating point type in little-endian order from the underlying reader. Read more
Source§

fn read_f64_le(&mut self) -> ReadF64Le<&mut Self>
where Self: Unpin,

Reads an 64-bit floating point type in little-endian order from the underlying reader. Read more
Source§

fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>
where Self: Unpin,

Reads all bytes until EOF in this source, placing them into buf. Read more
Source§

fn read_to_string<'a>( &'a mut self, dst: &'a mut String, ) -> ReadToString<'a, Self>
where Self: Unpin,

Reads all bytes until EOF in this source, appending them to buf. Read more
Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adaptor which reads at most limit bytes from it. Read more
Source§

impl<S> AsyncSeekExt for S
where S: AsyncSeek + ?Sized,

Source§

fn seek(&mut self, pos: SeekFrom) -> Seek<'_, Self>
where Self: Unpin,

Creates a future which will seek an IO object, and then yield the new position in the object and the object itself. Read more
Source§

fn rewind(&mut self) -> Seek<'_, Self>
where Self: Unpin,

Creates a future which will rewind to the beginning of the stream. Read more
Source§

fn stream_position(&mut self) -> Seek<'_, Self>
where Self: Unpin,

Creates a future which will return the current seek position from the start of the stream. 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.