Struct iowrap::ShortRead

source ·
pub struct ShortRead<R: Read, I: Iterator<Item = usize>> { /* private fields */ }
Expand description

Intentionally return short reads, to test Read code.

The decider iterator gets to decide how short a read should be. A read length of 0 generates an ErrorKind::Interrupted error. When the iterator runs out before the reader, read will always return zero-length reads (EOF).

Currently, no effort is made to make reads longer, if the underlying reader naturally returns short reads.

§Examples

Short read:

let mut naughty = iowrap::ShortRead::new(
        io::Cursor::new(b"1234567890"),
        vec![2, 3, 4, 5, 6].into_iter()
);
let mut buf = [0u8; 10];
// A `Cursor` would normally return the whole ten bytes here,
// but we've limited it to two bytes.
assert_eq!(2, naughty.read(&mut buf).unwrap());

Interrupted read:

let mut interrupting = iowrap::ShortRead::new(
        io::Cursor::new(b"123"),
        vec![0, 1, 0].into_iter()
);
let mut buf = [0u8; 10];
assert_eq!(io::ErrorKind::Interrupted,
        interrupting.read(&mut buf).unwrap_err().kind());

Implementations§

source§

impl<R: Read, I: Iterator<Item = usize>> ShortRead<R, I>

source

pub fn new(inner: R, decider: I) -> Self

source

pub fn into_inner(self) -> R

Trait Implementations§

source§

impl<R: Read, I: Iterator<Item = usize>> Read for ShortRead<R, I>

source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

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

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

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

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

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

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

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

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

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

Creates an adapter which will read at most limit bytes from it. Read more

Auto Trait Implementations§

§

impl<R, I> Freeze for ShortRead<R, I>
where R: Freeze, I: Freeze,

§

impl<R, I> RefUnwindSafe for ShortRead<R, I>

§

impl<R, I> Send for ShortRead<R, I>
where R: Send, I: Send,

§

impl<R, I> Sync for ShortRead<R, I>
where R: Sync, I: Sync,

§

impl<R, I> Unpin for ShortRead<R, I>
where R: Unpin, I: Unpin,

§

impl<R, I> UnwindSafe for ShortRead<R, I>
where R: UnwindSafe, I: 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<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> ReadMany for T
where T: Read,

source§

fn read_many(&mut self, buf: &mut [u8]) -> Result<usize, Error>

Try quite hard to fill buf with bytes. Read more
source§

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

§

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

§

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.