Skip to content

Instantly share code, notes, and snippets.

Created May 19, 2014 00:36
Show Gist options
  • Save anonymous/1f36060fd4e533ae0d8b to your computer and use it in GitHub Desktop.
Save anonymous/1f36060fd4e533ae0d8b to your computer and use it in GitHub Desktop.
// extern crate csv;
//
// use std::io;
//
// fn main() {
// let mut d = csv::Decoder::from_reader(box io::stdin() as Box<io::Reader>);
// for row in d.iter() {
// println!("{}", row);
// }
// }
struct Temp<'a> {
buf: std::io::BufferedReader<&'a mut std::io::Reader>
}
impl<'a> Temp<'a> {
fn from_reader(r: &mut std::io::Reader) -> Temp<'a> {
Temp::from_buffer(std::io::BufferedReader::new(r))
}
fn from_buffer(buf: std::io::BufferedReader<&'a mut std::io::Reader>) -> Temp<'a> {
Temp { buf: buf }
}
fn read_next_char(&mut self) -> Option<char> {
match self.buf.read_char() {
Ok(c) => Some(c),
Err(_) => None
}
}
}
fn main() {
// the following two lines work regardless of TTY/pipe:
let mut tmp = Temp::from_reader(&mut std::io::stdin());
// the following line does not work in 2 out of 3 ways:
//
// 1.) if typing directly into stdin:
// ./test
// a
// the program will print out the first letter of the input, then segfault before exiting
// 2.) if piping into stdin:
// echo 'asdf' | ./test
// the program crashes with:
// task '<main>' failed at 'assertion failed: (*self.data).count.load(Relaxed) > 0',
// /build/rust-git/src/rust/src/libstd/sync/arc.rs:90
// 3.) disable buffering of stdin via the unbuffer binary in the `expect` package will make
// the program work correctly again
// echo 'asdf' | unbuffer -p ./test
//let mut tmp = Temp::from_reader(&mut std::io::stdin());
println!("{}", tmp.read_next_char());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment