Trait handy_async::io::AsyncRead [] [src]

pub trait AsyncRead: Read + Sized {
    fn async_read<B: AsMut<[u8]>>(self, buf: B) -> ReadBytes<Self, B> { ... }
fn async_read_non_empty<B: AsMut<[u8]>>(
        self,
        buf: B
    ) -> ReadNonEmpty<Self, B> { ... }
fn async_read_exact<B: AsMut<[u8]>>(self, buf: B) -> ReadExact<Self, B> { ... } }

An asynchronous version of the standard Read trait.

Since this is assumed as a basic building block, it may be more convenient to use ReadPattern for ordinary cases.

Notice

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

Provided Methods

Creates a future which will read bytes asynchronously.

Examples

use std::io::empty;
use futures::Future;
use handy_async::io::AsyncRead;

let (_, buf, read_size) = b"hello".async_read([0; 8]).wait().ok().unwrap();
assert_eq!(read_size, 5);
assert_eq!(&buf[0..5], b"hello");

let (_, _, read_size) = empty().async_read([0; 4]).wait().ok().unwrap();
assert_eq!(read_size, 0);

Creates a future which will read non empty bytes asynchronously.

Examples

use std::io::{empty, ErrorKind};
use futures::Future;
use handy_async::io::AsyncRead;

let (_, buf, read_size) = b"hello".async_read_non_empty([0; 8]).wait().ok().unwrap();
assert_eq!(read_size, 5);
assert_eq!(&buf[0..5], b"hello");

let e = empty().async_read_non_empty([0; 4]).wait().err().unwrap();
assert_eq!(e.error_ref().kind(), ErrorKind::UnexpectedEof);

Creates a future which will read exact bytes asynchronously.

Examples

use std::io::ErrorKind;
use futures::Future;
use handy_async::io::AsyncRead;

let (_, buf) = b"hello".async_read_exact([0; 3]).wait().ok().unwrap();
assert_eq!(&buf[..], b"hel");

let e = b"hello".async_read_exact([0; 8]).wait().err().unwrap();
assert_eq!(e.error_ref().kind(), ErrorKind::UnexpectedEof);

Implementors