Trait handy_io::io::ReadFrom [] [src]

pub trait ReadFrom<R: Read>: Pattern {
    type Future: Future<Item = (R, Self::Value), Error = (R, Error)>;
    fn lossless_read_from(self, reader: R) -> Self::Future;

    fn read_from(self, reader: R) -> LossyReadFrom<R, Self::Future> { ... }
    fn sync_read_from(self, reader: R) -> Result<Self::Value> { ... }
    fn boxed(self) -> BoxReadFrom<R, Self::Value>
    where
        Self: Send + 'static,
        Self::Future: Send + 'static
, { ... } }

The ReadFrom trait allows for reading a value of the pattern from a source asynchronously.

Notice

For executing asynchronously, we assume the reader R returns the std::io::ErrorKind::WouldBlock error if a read operation would be about to block.

Associated Types

The future to read a value of the pattern from R.

Required Methods

Creates a future instance to read a value of the pattern from reader.

Provided Methods

Creates a future instance to read a value of the pattern from reader.

If the execution of the future fails, the reader will be dropped.

Example

extern crate futures;
extern crate handy_io;

use handy_io::io::ReadFrom;
use handy_io::pattern::{Pattern, Endian};
use handy_io::pattern::read::{U8, U16};
use futures::Future;

fn main() {
    let mut input = &[1, 0, 2][..];
    let pattern = (U8, U16.be());
    let future = pattern.read_from(&mut input);
    assert_eq!(future.wait().unwrap().1, (1, 2));
}

Scynchronously reading a value of the pattern from reader.

Example

use handy_io::io::ReadFrom;
use handy_io::pattern::{Pattern, Endian};
use handy_io::pattern::read::{U8, U16};

let pattern = (U8, U16.be());
assert_eq!(pattern.sync_read_from(&mut &[1, 0, 2][..]).unwrap(), (1, 2));

Returns the boxed version of this pattern.

Implementors