Add macro to manually flush a writer.

I built upon the pipe_* macros, adding pipe_flush!() to flush an
optional writer (default to stdout if no writer is given).
This commit is contained in:
Joseph Crail 2015-05-23 03:21:53 -04:00
parent 9ee7d96e5b
commit d558e37288
4 changed files with 38 additions and 2 deletions

View file

@ -162,6 +162,34 @@ macro_rules! pipe_writeln(
)
);
#[macro_export]
macro_rules! pipe_flush(
() => (
match ::std::io::stdout().flush() {
Ok(_) => true,
Err(f) => {
if f.kind() == ::std::io::ErrorKind::BrokenPipe {
false
} else {
panic!("{}", f)
}
}
}
);
($fd:expr) => (
match $fd.flush() {
Ok(_) => true,
Err(f) => {
if f.kind() == ::std::io::ErrorKind::BrokenPipe {
false
} else {
panic!("{}", f)
}
}
}
)
);
#[macro_export]
macro_rules! safe_write(
($fd:expr, $($args:tt)+) => (

View file

@ -12,10 +12,11 @@
extern crate getopts;
extern crate libc;
use std::io::{stdout, Write};
use std::io::Write;
use std::str::from_utf8;
#[path = "../common/util.rs"]
#[macro_use]
mod util;
#[allow(dead_code)]
@ -244,7 +245,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
}
if options.newline {
let _ = stdout().flush();
pipe_flush!();
} else {
println!("")
}

6
src/env/env.rs vendored
View file

@ -14,8 +14,13 @@
#![allow(non_camel_case_types)]
use std::env;
use std::io::Write;
use std::process::Command;
#[path = "../common/util.rs"]
#[macro_use]
mod util;
struct options {
ignore_env: bool,
null: bool,
@ -194,6 +199,7 @@ pub fn uumain(args: Vec<String>) -> i32 {
} else {
// no program provided
print_env(opts.null);
pipe_flush!();
}
0

View file

@ -256,4 +256,5 @@ fn print_seq(first: f64, step: f64, last: f64, largest_dec: usize, separator: St
if (first >= last && step < 0f64) || (first <= last && step > 0f64) {
pipe_print!("{}", terminator);
}
pipe_flush!();
}