Skip to main content

Reader

Struct Reader 

Source
pub struct Reader<R: ?Sized> { /* private fields */ }
Expand description

A reader, which can dynamically provide IO traits.

The following traits may be optionally dynamically provided:

The struct comes with a number of setter methods. The call to these requires proof to the compiler that the bound is met, inserting the vtable from the impl instance. Afterward, the bound is not required by any user. Using the (mutable) getters recombines the vtable with the underlying value.

Note that the value can not be unsized (dyn trait) itself. This may be fixed at a later point to make the reader suitable for use in embedded. In particular, the double indirection of instantiating with R = &mut dyn Read wouldn’t make sense as the setters would not be usable, their bounds can never be met. And combining traits into a large dyn-trait is redundant as it trait-impls become part of the static validity requirement again.

§Usage

let mut buffer: &[u8] = b"Hello, world!";
let mut reader = Reader::new(&mut buffer);
assert!(reader.as_buf().is_none());

// But slices are buffered readers, let's tell everyone.
reader.set_buf();
assert!(reader.as_buf().is_some());

// Now use the ReadBuf implementation directly
let buffered = reader.as_buf_mut().unwrap();
buffered.consume(7);
assert_eq!(buffered.fill_buf().unwrap(), b"world!");

Implementations§

Source§

impl<R: Read> Reader<R>

Source

pub fn new(reader: R) -> Self

Wrap an underlying reader by-value.

Source§

impl<R: ?Sized> Reader<R>

Source

pub fn get_ref(&self) -> &R

Provide access to the underlying reader.

Source

pub fn get_mut(&mut self) -> &mut R

Provide mutable access to the underlying reader.

Source

pub fn as_mut(&mut self) -> ReaderMut<'_>

Get a view equivalent to very-fat mutable reference.

This erases the concrete type R which allows consumers that intend to avoid polymorphic code that monomorphizes. The mutable reference has all accessors of a mutable reference except it doesn’t offer access with the underlying reader’s type itself.

Source

pub fn into_boxed<'lt>(self) -> ReaderBox<'lt>
where R: Sized + 'lt,

Get an allocated, type-erased very-fat mutable box.

This erases the concrete type R which allows consumers that intend to avoid polymorphic code that monomorphizes. The mutable reference has all accessors of a mutable reference except it doesn’t offer access with the underlying reader’s type itself.

Source§

impl<R> Reader<R>

Source

pub fn set_buf(&mut self)
where R: BufRead,

Set the V-Table for BufRead.

After this call, the methods Self::as_buf and Self::as_buf_mut will return values.

Source

pub fn set_seek(&mut self)
where R: Seek,

Set the V-Table for Seek.

After this call, the methods Self::as_seek and Self::as_seek_mut will return values.

Source

pub fn set_any(&mut self)
where R: Any,

Set the V-Table for Any.

After this call, the methods Self::as_any and Self::as_any_mut will return values.

Source§

impl<R> Reader<R>

Source

pub fn set_file_ext(&mut self)
where R: FileExt,

Set the V-Table for FileExt.

After this call, the methods Self::as_file_ext will return a value. (This trait only has methods with a &self receiver).

Source

pub fn set_as_fd(&mut self)
where R: AsFd,

Set the V-Table for AsRawFd.

After this call, the methods Self::as_fd will return a value. (This trait only has methods with a &self receiver).

Source

pub fn set_as_raw_fd(&mut self)
where R: AsRawFd,

Set the V-Table for AsRawFd.

After this call, the methods Self::as_raw_fd will return a value. (This trait only has methods with a &self receiver).

Source§

impl<R: ?Sized> Reader<R>

Source

pub fn as_read(&self) -> &(dyn Read + '_)

Get the inner value as a shared dynamic Read reference.

This is not terribly useful but provided for completeness.

Source

pub fn as_read_mut(&mut self) -> &mut (dyn Read + '_)

Get the inner value as a mutable dynamic Read reference.

Source

pub fn into_inner(self) -> R
where R: Sized,

Unwrap the inner value at its original sized type.

Source§

impl<R: ?Sized> Reader<R>

Source

pub fn as_buf(&self) -> Option<&dyn BufRead>

Get the inner value as a dynamic BufRead reference.

This returns None unless a previous call to Self::set_buf as executed, by any other caller. The value can be moved after such call arbitrarily.

Source

pub fn as_buf_mut(&mut self) -> Option<&mut dyn BufRead>

Get the inner value as a mutable dynamic BufRead reference.

This returns None unless a previous call to Self::set_buf as executed, by any other caller. The value can be moved after such call arbitrarily.

Source

pub fn as_seek(&self) -> Option<&dyn Seek>

Get the inner value as a dynamic Seek reference.

This returns None unless a previous call to Self::set_seek as executed, by any other caller. The value can be moved after such call arbitrarily.

Source

pub fn as_seek_mut(&mut self) -> Option<&mut dyn Seek>

Get the inner value as a mutable dynamic Seek reference.

This returns None unless a previous call to Self::set_seek as executed, by any other caller. The value can be moved after such call arbitrarily.

Source

pub fn as_any(&self) -> Option<&dyn Any>

Get the inner value as a dynamic Any reference.

Source

pub fn as_any_mut(&mut self) -> Option<&mut dyn Any>

Get the inner value as a dynamic Any reference.

Source§

impl<R: ?Sized> Reader<R>

Source

pub fn as_file_ext(&self) -> Option<&dyn FileExt>

Get the inner value as a dynamic FileExt reference.

Source

pub fn as_fd(&self) -> Option<&dyn AsFd>

Get the inner value as a dynamic AsFd reference.

Source

pub fn as_raw_fd(&self) -> Option<&dyn AsRawFd>

Get the inner value as a dynamic AsRawFd reference.

Trait Implementations§

Source§

impl<'lt, R> From<&'lt mut Reader<R>> for ReaderMut<'lt>

Source§

fn from(value: &'lt mut Reader<R>) -> Self

Converts to this type from the input type.
Source§

impl<'lt, R: 'lt> From<Reader<R>> for ReaderBox<'lt>

Source§

fn from(value: Reader<R>) -> Self

Converts to this type from the input type.
Source§

impl<R: Read> Read for Reader<R>

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

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

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

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

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

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

Reads all bytes until EOF in this source, appending them to buf. 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
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> 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<'_, u8>, ) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads 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” adapter 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
Source§

fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
where Self: Sized,

🔬This is a nightly-only experimental API. (read_array)
Read and return a fixed array of bytes from this source. Read more
Source§

fn read_le<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in little-endian order. Read more
Source§

fn read_be<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in big-endian order. Read more

Auto Trait Implementations§

§

impl<R> !RefUnwindSafe for Reader<R>

§

impl<R> !Send for Reader<R>

§

impl<R> !Sync for Reader<R>

§

impl<R> !UnwindSafe for Reader<R>

§

impl<R> Freeze for Reader<R>
where R: Freeze + ?Sized,

§

impl<R> Unpin for Reader<R>
where R: Unpin + ?Sized,

§

impl<R> UnsafeUnpin for Reader<R>
where R: UnsafeUnpin + ?Sized,

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.