diff options
author | Ben Bridle <bridle.benjamin@gmail.com> | 2023-11-26 10:29:33 +1300 |
---|---|---|
committer | Ben Bridle <bridle.benjamin@gmail.com> | 2023-11-26 10:33:01 +1300 |
commit | 5f2b0b00b2dea0367b3ab6bfcae7d17b2fdf3ebd (patch) | |
tree | 8ec86b8de0a531b940f26489d7bf9d8c4086359f /src | |
parent | 8dfdd603b022ab9775cda08b9c2350d43667ff0f (diff) | |
download | bedrock-asm-5f2b0b00b2dea0367b3ab6bfcae7d17b2fdf3ebd.zip |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 18 |
1 files changed, 5 insertions, 13 deletions
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); } } |