Trait mycelium_util::io::Read

source ·
pub trait Read {
    // Required method
    fn read(&mut self, buf: &mut [u8]) -> Result<usize>;

    // Provided methods
    unsafe fn initializer(&self) -> Initializer { ... }
    fn read_exact(&mut self, buf: &mut [u8]) -> Result<()> { ... }
    fn by_ref(&mut self) -> &mut Self
       where Self: Sized { ... }
    fn bytes(self) -> Bytes<Self> 
       where Self: Sized { ... }
    fn chain<R: Read>(self, next: R) -> Chain<Self, R>
       where Self: Sized { ... }
    fn take(self, limit: u64) -> Take<Self>
       where Self: Sized { ... }
}
Expand description

The Read trait allows for reading bytes from a source.

Implementors of the Read trait are called ‘readers’.

Readers are defined by one required method, read(). Each call to read() will attempt to pull bytes from this source into a provided buffer. A number of other methods are implemented in terms of read(), giving implementors a number of ways to read bytes while only needing to implement a single method.

This is essentially a vendored version of the std::io::Read trait from the Rust standard library, modified to work without std. See the module-level docs for mycelium_util::io for more information on how mycelium_util’s io module differs from std’s.

Required Methods§

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.

This function does not provide any guarantees about whether it blocks waiting for data, but if an object needs to block for a read but cannot it will typically signal this via an Err return value.

If the return value of this method is [Ok(n)], then it must be guaranteed that 0 <= n <= buf.len(). A nonzero n value indicates that the buffer buf has been filled in with n bytes of data from this source. If n is 0, then it can indicate one of two scenarios:

  1. This reader has reached its “end of file” and will likely no longer be able to produce bytes. Note that this does not mean that the reader will always no longer be able to produce bytes.
  2. The buffer specified was 0 bytes in length.

No guarantees are provided about the contents of buf when this function is called, implementations cannot rely on any property of the contents of buf being true. It is recommended that implementations only write data to buf instead of reading its contents.

Correspondingly, however, callers of this method may not assume any guarantees about how the implementation uses buf. The trait is safe to implement, so it is possible that the code that’s supposed to write to the buffer might also read from it. It is your responsibility to make sure that buf is initialized before calling read. Calling read with an uninitialized buf (of the kind one obtains via MaybeUninit<T>) is not safe, and can lead to undefined behavior.

§Errors

If this function encounters any form of I/O or other error, an error variant will be returned. If an error is returned then it must be guaranteed that no bytes were read.

An error of the ErrorKind::Interrupted kind is non-fatal and the read operation should be retried if there is nothing else to do.

Provided Methods§

source

unsafe fn initializer(&self) -> Initializer

Determines if this Reader can work with buffers of uninitialized memory.

The default implementation returns an initializer which will zero buffers.

If a Reader guarantees that it can work properly with uninitialized memory, it should call Initializer::nop(). See the documentation for Initializer for details.

The behavior of this method must be independent of the state of the Reader - the method only takes &self so that it can be used through trait objects.

§Safety

This method is unsafe because a Reader could otherwise return a non-zeroing Initializer from another Read type without an unsafe block.

source

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

Read the exact number of bytes required to fill buf.

This function reads as many bytes as necessary to completely fill the specified buffer buf.

No guarantees are provided about the contents of buf when this function is called, implementations cannot rely on any property of the contents of buf being true. It is recommended that implementations only write data to buf instead of reading its contents.

§Errors

If this function encounters an error of the kind ErrorKind::Interrupted then the error is ignored and the operation will continue.

If this function encounters an “end of file” before completely filling the buffer, it returns an error of the kind ErrorKind::UnexpectedEof. The contents of buf are unspecified in this case.

If any other read error is encountered then this function immediately returns. The contents of buf are unspecified in this case.

If this function returns an error, it is unspecified how many bytes it has read, but it will never read more than would be necessary to completely fill the buffer.

source

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

Creates a “by reference” adaptor for this instance of Read.

The returned adaptor also implements Read and will simply borrow this current reader.

source

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

Transforms this Read instance to an Iterator over its bytes.

The returned type implements Iterator where the Item is Result<u8, io::Error>. The yielded item is Ok if a byte was successfully read and Err otherwise. EOF is mapped to returning None from this iterator.

source

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

Creates an adaptor which will chain this stream with another.

The returned Read instance will first read all bytes from this object until EOF is encountered. Afterwards the output is equivalent to the output of next.

§Examples

Files implement Read:

use std::io;
use std::io::prelude::*;
use std::fs::File;

fn main() -> io::Result<()> {
    let mut f1 = File::open("foo.txt")?;
    let mut f2 = File::open("bar.txt")?;

    let mut handle = f1.chain(f2);
    let mut buffer = String::new();

    // read the value into a String. We could use any Read method here,
    // this is just one example.
    handle.read_to_string(&mut buffer)?;
    Ok(())
}
source

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

Creates an adaptor which will read at most limit bytes from it.

This function returns a new instance of Read which will read at most limit bytes, after which it will always return EOF ([Ok(0)]). Any read errors will not count towards the number of bytes read and future calls to read() may succeed.

Implementations on Foreign Types§

source§

impl Read for &[u8]

Read is implemented for &[u8] by copying from the slice.

Note that reading updates the slice to point to the yet unread part. The slice will be empty when EOF is reached.

source§

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

source§

unsafe fn initializer(&self) -> Initializer

source§

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

source§

impl<R: Read + ?Sized> Read for &mut R

source§

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

source§

unsafe fn initializer(&self) -> Initializer

Implementors§

source§

impl Read for Empty

source§

impl Read for Repeat

source§

impl<T> Read for Cursor<T>
where T: AsRef<[u8]>,

source§

impl<T: Read> Read for Take<T>

source§

impl<T: Read, U: Read> Read for Chain<T, U>