Skip to content

Commit e81feb1

Browse files
author
oech3
committed
tee: simplify impl for flush
1 parent b8d26ad commit e81feb1

1 file changed

Lines changed: 15 additions & 23 deletions

File tree

src/uu/tee/src/tee.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,16 @@ impl MultiWriter {
197197

198198
fn write_flush(&mut self, buf: &[u8]) -> Result<()> {
199199
let mode = self.output_error_mode;
200-
self.writers.retain_mut(|writer| {
201-
let res = (|| {
202-
writer.inner.write_all(buf)?;
203-
writer.inner.flush()
204-
})();
205-
match res {
200+
self.writers
201+
.retain_mut(|writer| match writer.inner.write_all(buf) {
206202
Ok(()) => true,
207203
Err(e) => {
208204
if let Err(e) = process_error(mode, e, writer, &mut self.ignored_errors) {
209205
self.aborted.get_or_insert(e);
210206
}
211207
false
212208
}
213-
}
214-
});
209+
});
215210
self.aborted.take().map_or(
216211
if self.writers.is_empty() {
217212
// This error kind will never be raised by the standard
@@ -259,10 +254,9 @@ impl Write for Writer {
259254
fn write(&mut self, buf: &[u8]) -> Result<usize> {
260255
// raw syscall avoids buffering which is POSIX requirement
261256
// better throughput by unknown reason...
262-
use rustix::io::write;
263257
match self {
264-
Self::File(f) => Ok(write(f, buf)?),
265-
Self::Stdout(s) => Ok(write(s, buf)?),
258+
Self::File(f) => Ok(rustix::io::write(f, buf)?),
259+
Self::Stdout(s) => Ok(rustix::io::write(s, buf)?),
266260
}
267261
}
268262

@@ -271,19 +265,17 @@ impl Write for Writer {
271265
}
272266
}
273267

274-
#[cfg(not(unix))] // todo: investigate how to improve throughput
275-
impl Write for Writer {
276-
fn write(&mut self, buf: &[u8]) -> Result<usize> {
268+
#[cfg(not(unix))] // todo: investigate how to remove flush overhead
269+
impl Writer {
270+
// override write_all to avoid calling many flush
271+
pub fn write_all(&mut self, buf: &[u8]) -> Result<()> {
277272
match self {
278-
Self::File(f) => f.write(buf),
279-
Self::Stdout(s) => s.write(buf),
280-
}
281-
}
282-
283-
fn flush(&mut self) -> Result<()> {
284-
match self {
285-
Self::File(f) => f.flush(),
286-
Self::Stdout(s) => s.flush(),
273+
// File does not have line buffering
274+
Self::File(f) => f.write_all(buf),
275+
Self::Stdout(s) => {
276+
s.write_all(buf)?;
277+
s.flush()
278+
}
287279
}
288280
}
289281
}

0 commit comments

Comments
 (0)