Struct flexible_io::reader::Reader

source ·
pub struct Reader<R> { /* 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> 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 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§

impl<R> Reader<R>

source

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

Get the inner value as a dynamic Read reference.

source

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

Get the inner value as a mutable dynamic Read reference.

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.

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.

Auto Trait Implementations§

§

impl<R> !RefUnwindSafe for Reader<R>

§

impl<R> !Send for Reader<R>

§

impl<R> !Sync for Reader<R>

§

impl<R> Unpin for Reader<R>
where R: Unpin,

§

impl<R> !UnwindSafe for Reader<R>

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

§

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.