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 (this behavior differs from tokio_io::io::lines).

This example is not tested
impl Codec for DelimCodec<u8>;
impl Codec for DelimCodec<Newline>;
impl Codec for DelimCodec<impl Into<u8> + Clone>;

Example: read stdin line by line

extern crate futures;
extern crate tokio;
extern crate tokio_io;
extern crate tokio_file_unix;

use futures::{Future, Stream};
use tokio_io::codec::FramedRead;

// get the standard input as a file
let stdin = tokio_file_unix::raw_stdin()?;
let file = tokio_file_unix::File::new_nb(stdin)?;
let io = file.into_io(&tokio::reactor::Handle::default())?;

// turn it into a stream of lines, decoded as UTF-8
let codec = tokio_file_unix::DelimCodec(tokio_file_unix::Newline);
let line_stream = FramedRead::new(io, codec).and_then(|line| {
    String::from_utf8(line).map_err(|_| {
        std::io::Error::from(std::io::ErrorKind::InvalidData)
    })
});

// turn it into a stream of lines and process them
let future = line_stream.for_each(|line| {
    println!("Got: {}", line);
    Ok(())
}).map_err(|e| panic!("{:?}", e));

// start the event loop
tokio::run(future);

Trait Implementations

impl<D: Debug> Debug for DelimCodec<D>
[src]

[src]

Formats the value using the given formatter. Read more

impl<D: Clone> Clone for DelimCodec<D>
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<D: Copy> Copy for DelimCodec<D>
[src]

impl<D: Into<u8> + Clone> Decoder for DelimCodec<D>
[src]

The type of decoded frames.

The type of unrecoverable frame decoding errors. Read more

[src]

Attempts to decode a frame from the provided buffer of bytes. Read more

[src]

A default method available to be called when there are no more bytes available to be read from the underlying I/O. Read more

impl<D: Into<u8> + Clone> Encoder for DelimCodec<D>
[src]

The type of items consumed by the Encoder

The type of encoding errors. Read more

[src]

Encodes a frame into the buffer provided. Read more

Auto Trait Implementations

impl<D> Send for DelimCodec<D> where
    D: Send

impl<D> Sync for DelimCodec<D> where
    D: Sync