Trait handy_async::io::WriteInto
[−]
[src]
pub trait WriteInto<W: Write>: AsyncMatch<PatternWriter<W>> { fn write_into(self, writer: W) -> WritePattern<Self, W> { ... } fn sync_write_into(self, writer: W) -> Result<Self::Value> { ... } }
The WriteInto
trait allows for writing
a value of this pattern to a sink asynchronously.
Notice
For executing asynchronously, we assume the writer W
returns
the std::io::ErrorKind::WouldBlock
error if a write operation would be about to block.
Examples
Defines your own writing pattern:
use std::io::Write; use futures::{Future, BoxFuture}; use handy_async::io::{WriteInto, PatternWriter, AsyncIoError}; use handy_async::pattern::Pattern; use handy_async::matcher::AsyncMatch; // Defines pattern. struct HelloWorld; impl Pattern for HelloWorld { type Value = (); } // Implements pattern maching between `PatternWriter<W>` and `HelloWorld`. impl<W: Write + Send + 'static> AsyncMatch<PatternWriter<W>> for HelloWorld { type Future = BoxFuture<(PatternWriter<W>, ()), AsyncIoError<PatternWriter<W>>>; fn async_match(self, matcher: PatternWriter<W>) -> Self::Future { Vec::from(&b"Hello World!"[..]).map(|_| ()).async_match(matcher).boxed() } } // Executes pattern matching. let pattern = ("Hey! ".to_string(), HelloWorld); let (output, _) = pattern.write_into(Vec::new()).wait().unwrap(); assert_eq!(output, b"Hey! Hello World!");
Provided Methods
fn write_into(self, writer: W) -> WritePattern<Self, W>
Creates a future instance to write a value of the pattern to writer
.
Examples
use futures::Future; use handy_async::io::WriteInto; use handy_async::pattern::Endian; let pattern = (1u8, 2u16.be()); let (output, _) = pattern.write_into(Vec::new()).wait().unwrap(); assert_eq!(output, [1, 0, 2]);
fn sync_write_into(self, writer: W) -> Result<Self::Value>
Synchronous version of the WriteInto::write_into
method.
Implementors
impl<W: Write, T> WriteInto<W> for T where
T: AsyncMatch<PatternWriter<W>>,