From a8c4a33e918dac301cbc8fb197200b34df4c4d6c Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 23 Jun 2023 02:32:29 -0700 Subject: [PATCH] Avoid using trait object --- native/src/core/logging.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/native/src/core/logging.rs b/native/src/core/logging.rs index 0e33befa7..cd6223b34 100644 --- a/native/src/core/logging.rs +++ b/native/src/core/logging.rs @@ -212,11 +212,25 @@ enum LogFile<'a> { Actual(File), } -impl LogFile<'_> { - fn as_write(&mut self) -> &mut dyn Write { +impl Write for LogFile<'_> { + fn write(&mut self, buf: &[u8]) -> io::Result { match self { - Buffer(e) => e, - Actual(ref mut e) => e, + Buffer(e) => e.write(buf), + Actual(e) => e.write(buf), + } + } + + fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { + match self { + Buffer(e) => e.write_vectored(bufs), + Actual(e) => e.write_vectored(bufs), + } + } + + fn flush(&mut self) -> io::Result<()> { + match self { + Buffer(e) => e.flush(), + Actual(e) => e.flush(), } } } @@ -303,7 +317,7 @@ extern "C" fn logfile_writer(arg: *mut c_void) -> *mut c_void { let io2 = IoSlice::new(msg); // We don't need to care the written len because we are writing less than PIPE_BUF // It's guaranteed to always write the whole thing atomically - let _ = logfile.as_write().write_vectored(&[io1, io2])?; + let _ = logfile.write_vectored(&[io1, io2])?; } }