Skip to content

Commit c0509eb

Browse files
committed
Flush stdout on read from stdin again
This is possible now since we have reentrant mutexes. Fixes rust-lang#25555.
1 parent 84b1e08 commit c0509eb

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

src/libstd/io/stdio.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ impl Stdin {
165165
#[stable(feature = "rust1", since = "1.0.0")]
166166
impl Read for Stdin {
167167
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
168+
// Flush stdout so that weird issues like a print!'d prompt not being
169+
// shown until after the user hits enter.
170+
drop(stdout().flush());
168171
self.lock().read(buf)
169172
}
170173
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use std::env;
12+
use std::io;
13+
use std::io::{Read, Write};
14+
use std::process::{Command, Stdio};
15+
16+
fn main(){
17+
if env::args().count() > 1 && env::args().nth(1) == Some("child".to_string()) {
18+
child()
19+
} else {
20+
let mut p = Command::new(env::args().nth(0).unwrap())
21+
.arg("child")
22+
.stdin(Stdio::piped())
23+
.stdout(Stdio::piped())
24+
.stderr(Stdio::inherit())
25+
.spawn().unwrap();
26+
{
27+
let mut buf = [0; 1];
28+
assert!(p.stdout.as_mut().unwrap().read(&mut buf).unwrap() >= 1);
29+
assert_eq!(buf[0], b'>');
30+
assert!(p.stdin.as_mut().unwrap().write(b"abcd\n").unwrap() >= 1);
31+
}
32+
// FIXME(#25572): timeout and fail on timeout
33+
assert!(p.wait().unwrap().success());
34+
}
35+
}
36+
37+
fn child(){
38+
let stdout = io::stdout();
39+
let lstdout = stdout.lock();
40+
let mut stdin = io::stdin();
41+
print!(">");
42+
let mut letter = [0; 1];
43+
stdin.read(&mut letter).unwrap();
44+
}

0 commit comments

Comments
 (0)