Skip to content

tokio::io::Stdin implements AsyncRead but is not Async #633

@aep

Description

@aep

Version

tokio 0.1.8
futures 0.1.21

Platform

Linux sdzx 4.18.5-arch1-1-ARCH #1 SMP PREEMPT Fri Aug 24 12:48:58 UTC 2018 x86_64 GNU/Linux

Description

poll_read on tokio::io::Stdin is blocking. that is unexpected and confusing.

extern crate tokio;
#[macro_use]
extern crate futures;

use tokio::io::{AsyncRead,stdin};
use std::io::{Write, stdout};
use futures::{Future, Async};

struct IoBridge<R,W>
where R: AsyncRead,
      W: Write,
{
    r: R,
    w: W,
}


impl<R,W> Future for IoBridge<R,W>
where R: AsyncRead,
      W: Write,
{
    type Item  = ();
    type Error = tokio::io::Error;
    fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
        println!(".");
        let mut buf = [0;1024];
        loop {
            let len = try_ready!(self.r.poll_read(&mut buf));
            println!("<< {}", len);
            if len == 0 {
                return Ok(Async::Ready(()));
            }
            self.w.write(&buf[..len])?;
            self.w.flush()?;
        }
    }
}


fn main() {
    let f = IoBridge{
        r: stdin(),
        w: stdout()
    }.map_err(|e|panic!(e));

    tokio::run(f);
}

note that . is only printed once. the loop will never exit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions