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> Reader<R>
impl<R> Reader<R>
sourcepub fn as_mut(&mut self) -> ReaderMut<'_>
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.
sourcepub fn set_buf(&mut self)where
R: BufRead,
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.
sourcepub fn set_seek(&mut self)where
R: Seek,
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>
impl<R> Reader<R>
sourcepub fn as_read_mut(&mut self) -> &mut (dyn Read + '_)
pub fn as_read_mut(&mut self) -> &mut (dyn Read + '_)
Get the inner value as a mutable dynamic Read reference.
sourcepub fn as_buf(&self) -> Option<&(dyn BufRead + '_)>
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.
sourcepub fn as_buf_mut(&mut self) -> Option<&mut (dyn BufRead + '_)>
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.
sourcepub fn as_seek(&self) -> Option<&(dyn Seek + '_)>
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.
sourcepub fn as_seek_mut(&mut self) -> Option<&mut (dyn Seek + '_)>
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.