From 5f2b0b00b2dea0367b3ab6bfcae7d17b2fdf3ebd Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Sun, 26 Nov 2023 10:29:33 +1300 Subject: Improve reading and writing from stdin/stdout Previously, attempting to write large amounts of bytecode to stdout would often result in only the first n bytes being written. --- src/main.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 8d6e186..ead1268 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,7 @@ use bedrock_asm::*; fn main() { // Read source code from standard input let mut source_code = String::new(); - let mut stdin = std::io::stdin().lock(); - if let Err(err) = stdin.read_to_string(&mut source_code) { + if let Err(err) = std::io::stdin().read_to_string(&mut source_code) { eprintln!("Could not read from standard input, quitting."); eprintln!("({err:?})"); std::process::exit(1); @@ -17,23 +16,16 @@ fn main() { if token.print_error(&source_code) { is_error = true }; } eprintln!("Assembled program in {} bytes.", bytecode.len()); - let bytecode_len = bytecode.len(); if is_error { std::process::exit(1) } // Write bytecode to standard output - let mut stdout = std::io::stdout().lock(); - match stdout.write(&bytecode) { - Ok(len) => if len != bytecode_len { - eprintln!("Only wrote {len} of {bytecode_len} bytes") - } - Err(err) => { - eprintln!("Could not write to standard output, quitting."); - eprintln!("({err:?})"); - std::process::exit(1); - } + if let Err(err) = std::io::stdout().write_all(&bytecode) { + eprintln!("Could not write to standard output, quitting."); + eprintln!("({err:?})"); + std::process::exit(1); } } -- cgit v1.2.3-70-g09d2