fix test code

mio-0-7
k-nasa 5 years ago
parent b95bd6c1fe
commit ec4b09ecd0

@ -1,10 +1,12 @@
use futures::select; use futures::select;
use futures::FutureExt; use futures::FutureExt;
use std::io::{self, BufReader as StdBufReader, BufRead};
use async_std::{ use async_std::{
io::{stdin, BufReader}, io::{BufReader},
net::{TcpStream, ToSocketAddrs}, net::{TcpStream, ToSocketAddrs},
prelude::*, prelude::*,
stream,
task, task,
}; };
@ -20,8 +22,9 @@ async fn try_main(addr: impl ToSocketAddrs) -> Result<()> {
let reader = BufReader::new(reader); let reader = BufReader::new(reader);
let mut lines_from_server = futures::StreamExt::fuse(reader.lines()); let mut lines_from_server = futures::StreamExt::fuse(reader.lines());
let stdin = BufReader::new(stdin()); let stdin = StdBufReader::new(io::stdin());
let mut lines_from_stdin = futures::StreamExt::fuse(stdin.lines()); let mut lines_from_stdin = stream::from_iter(stdin.lines());
loop { loop {
select! { select! {
line = lines_from_server.next().fuse() => match line { line = lines_from_server.next().fuse() => match line {

@ -1,6 +1,7 @@
//! Prints a file given as an argument to stdout. //! Prints a file given as an argument to stdout.
use std::env::args; use std::env::args;
use std::io::Write;
use async_std::fs::File; use async_std::fs::File;
use async_std::io; use async_std::io;
@ -14,7 +15,7 @@ fn main() -> io::Result<()> {
task::block_on(async { task::block_on(async {
let mut file = File::open(&path).await?; let mut file = File::open(&path).await?;
let mut stdout = io::stdout(); let mut stdout = std::io::stdout();
let mut buf = vec![0u8; LEN]; let mut buf = vec![0u8; LEN];
loop { loop {
@ -23,12 +24,12 @@ fn main() -> io::Result<()> {
// If this is the end of file, clean up and return. // If this is the end of file, clean up and return.
if n == 0 { if n == 0 {
stdout.flush().await?; stdout.flush()?;
return Ok(()); return Ok(());
} }
// Write the buffer into stdout. // Write the buffer into stdout.
stdout.write_all(&buf[..n]).await?; stdout.write_all(&buf[..n])?;
} }
}) })
} }

@ -1,18 +1,18 @@
//! Echoes lines read on stdin to stdout. //! Echoes lines read on stdin to stdout.
use std::io::Write;
use async_std::io; use async_std::io;
use async_std::prelude::*;
use async_std::task; use async_std::task;
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
task::block_on(async { task::block_on(async {
let stdin = io::stdin(); let stdin = std::io::stdin();
let mut stdout = io::stdout(); let mut stdout = std::io::stdout();
let mut line = String::new(); let mut line = String::new();
loop { loop {
// Read a line from stdin. // Read a line from stdin.
let n = stdin.read_line(&mut line).await?; let n = stdin.read_line(&mut line)?;
// If this is the end of stdin, return. // If this is the end of stdin, return.
if n == 0 { if n == 0 {
@ -20,8 +20,8 @@ fn main() -> io::Result<()> {
} }
// Write the line to stdout. // Write the line to stdout.
stdout.write_all(line.as_bytes()).await?; stdout.write_all(line.as_bytes())?;
stdout.flush().await?; stdout.flush()?;
line.clear(); line.clear();
} }
}) })

@ -8,11 +8,11 @@ use async_std::task;
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
// This async scope times out after 5 seconds. // This async scope times out after 5 seconds.
task::block_on(io::timeout(Duration::from_secs(5), async { task::block_on(io::timeout(Duration::from_secs(5), async {
let stdin = io::stdin(); let stdin = std::io::stdin();
// Read a line from the standard input and display it. // Read a line from the standard input and display it.
let mut line = String::new(); let mut line = String::new();
stdin.read_line(&mut line).await?; stdin.read_line(&mut line)?;
dbg!(line); dbg!(line);
Ok(()) Ok(())

@ -8,9 +8,9 @@ use async_std::task;
fn io_timeout_timedout() { fn io_timeout_timedout() {
task::block_on(async { task::block_on(async {
io::timeout(Duration::from_secs(1), async { io::timeout(Duration::from_secs(1), async {
let stdin = io::stdin(); let stdin = std::io::stdin();
let mut line = String::new(); let mut line = String::new();
let _n = stdin.read_line(&mut line).await?; let _n = stdin.read_line(&mut line)?;
Ok(()) Ok(())
}) })
.await .await

Loading…
Cancel
Save