Struct tokio_file_unix::DelimCodec
[−]
[src]
pub struct DelimCodec<D>(pub D);
A Codec
that splits the stream into frames divided by a given delimiter
byte. All frames except possibly the last one contain the delimiter byte
as the last element.
impl Codec for DelimCodec<u8> impl Codec for DelimCodec<Newline> impl Codec for DelimCodec<impl Into<u8> + Copy>
Example: read stdin line by line
extern crate futures; extern crate tokio_core; use futures::Stream; use tokio_core::io::Io; // initialize the event loop let mut core = tokio_core::reactor::Core::new()?; let handle = core.handle(); // get the standard input as a file let stdin = std::io::stdin(); let io = File::new_nb(StdFile(stdin.lock()))?.into_io(&handle)?; // turn it into a stream of lines, decoded as UTF-8 let line_stream = io.framed(DelimCodec(Newline)).and_then(|line| { String::from_utf8(line).map_err(|_| { std::io::Error::from(std::io::ErrorKind::InvalidData) }) }); // specify how each line is to be processed let future = line_stream.for_each(|line| { println!("Got: {}", line); Ok(()) }); // start the event loop core.run(future)?;
Trait Implementations
impl<D: Debug> Debug for DelimCodec<D>
[src]
impl<D: Clone> Clone for DelimCodec<D>
[src]
fn clone(&self) -> DelimCodec<D>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<D: Copy> Copy for DelimCodec<D>
[src]
impl<D: Into<u8> + Copy> Codec for DelimCodec<D>
[src]
type In = Vec<u8>
The type of decoded frames.
type Out = Vec<u8>
The type of frames to be encoded.
fn decode(&mut self, buf: &mut EasyBuf) -> Result<Option<Self::In>>
Attempts to decode a frame from the provided buffer of bytes. Read more
fn decode_eof(&mut self, buf: &mut EasyBuf) -> Result<Self::In>
A default method available to be called when there are no more bytes available to be read from the underlying I/O. Read more
fn encode(&mut self, msg: Self::Out, buf: &mut Vec<u8>) -> Result<()>
Encodes a frame into the buffer provided. Read more