Use fmt::Write instead of io::Write in write.rs.

It is common to represent a function as a String, and previously that
required re-validating the UTF-8 in a Vec<u8>. The fmt::Write trait
writes UTF-8 directly into a String, so no extra checking is required.

This also means we can implement Display for Function which gives it a
to_string() method. This makes the function_to_string() method
redundant, so delete it.

The functions in the write module are no longer generally useful, so
make the module private. The Display trait on Function is all we need.
This commit is contained in:
Jakob Stoklund Olesen
2016-09-15 13:56:42 -07:00
parent 0b8bf530b0
commit b40a3495fe
4 changed files with 21 additions and 30 deletions

View File

@@ -3,11 +3,9 @@
//! Read a sequence of Cretonne IL files and print them again to stdout. This has the effect of
//! normalizing formatting and removing comments.
use std::io;
use CommandResult;
use utils::read_to_string;
use cton_reader::parse_functions;
use cretonne::write::write_function;
pub fn run(files: Vec<String>) -> CommandResult {
for (i, f) in files.into_iter().enumerate() {
@@ -27,9 +25,7 @@ fn cat_one(filename: String) -> CommandResult {
if idx != 0 {
println!("");
}
let stdout = io::stdout();
let mut handle = stdout.lock();
try!(write_function(&mut handle, &func).map_err(|e| format!("{}: {}", filename, e)));
print!("{}", func);
}
Ok(())