Trait handy_async::io::AsyncWrite [] [src]

pub trait AsyncWrite: Write + Sized {
    fn async_write<B: AsRef<[u8]>>(self, buf: B) -> WriteBytes<Self, B> { ... }
fn async_write_all<B: AsRef<[u8]>>(self, buf: B) -> WriteAll<Self, B> { ... }
fn async_flush(self) -> Flush<Self> { ... } }

An asynchronous version of the standard Write trait.

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

Notice

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

Provided Methods

Creates a future which will write bytes asynchronously.

Examples

use futures::Future;
use handy_async::io::AsyncWrite;

let (output, _, _) = vec![].async_write(b"hello").wait().ok().unwrap();
assert_eq!(&output[..], b"hello");

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

let (_, _, written_size) = [0; 0].async_write(b"hello").wait().ok().unwrap();
assert_eq!(written_size, 0);

Creates a future which will write all bytes asynchronously.

Examples

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

let (output, _) = vec![].async_write_all(b"hello").wait().ok().unwrap();
assert_eq!(&output[..], b"hello");

let mut output = [0; 3];
let e = (&mut output).async_write_all(b"hello").wait().err().unwrap();
assert_eq!(e.error_ref().kind(), ErrorKind::UnexpectedEof);

Creates a future which will flush the internal buffer.

Examples

use std::io::BufWriter;
use futures::Future;
use handy_async::io::AsyncWrite;

let writer = BufWriter::new(vec![]);
let (writer, _) = writer.async_write_all(b"hello").wait().ok().unwrap();
assert_eq!(writer.get_ref(), b"");

let writer = writer.async_flush().wait().ok().unwrap();
assert_eq!(writer.get_ref(), b"hello");

Implementors