From 28db5214d2c48e7a58cf79b9e272097260910a33 Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Wed, 2 Sep 2020 23:25:09 +0200 Subject: [PATCH 1/4] More implementations of Write for immutable refs Fixes #73836 --- library/std/src/io/stdio.rs | 54 +++++++++++++++++++++++++++++++++++++ library/std/src/io/util.rs | 24 +++++++++++++++++ library/std/src/process.rs | 19 +++++++++++++ 3 files changed, 97 insertions(+) diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index 3943c66aad53a..f320cf907c365 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -606,6 +606,33 @@ impl Write for Stdout { self.lock().write_fmt(args) } } + +#[stable(feature = "write_mt", since = "1.47.0")] +impl Write for &Stdout { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.lock().write(buf) + } + fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { + self.lock().write_vectored(bufs) + } + #[inline] + fn is_write_vectored(&self) -> bool { + self.lock().is_write_vectored() + } + fn flush(&mut self) -> io::Result<()> { + self.lock().flush() + } + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + self.lock().write_all(buf) + } + fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> { + self.lock().write_all_vectored(bufs) + } + fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> { + self.lock().write_fmt(args) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Write for StdoutLock<'_> { fn write(&mut self, buf: &[u8]) -> io::Result { @@ -782,6 +809,33 @@ impl Write for Stderr { self.lock().write_fmt(args) } } + +#[stable(feature = "write_mt", since = "1.47.0")] +impl Write for &Stderr { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.lock().write(buf) + } + fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { + self.lock().write_vectored(bufs) + } + #[inline] + fn is_write_vectored(&self) -> bool { + self.lock().is_write_vectored() + } + fn flush(&mut self) -> io::Result<()> { + self.lock().flush() + } + fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { + self.lock().write_all(buf) + } + fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> { + self.lock().write_all_vectored(bufs) + } + fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> { + self.lock().write_fmt(args) + } +} + #[stable(feature = "rust1", since = "1.0.0")] impl Write for StderrLock<'_> { fn write(&mut self, buf: &[u8]) -> io::Result { diff --git a/library/std/src/io/util.rs b/library/std/src/io/util.rs index a093b745b0c13..ce4dd1dcd493d 100644 --- a/library/std/src/io/util.rs +++ b/library/std/src/io/util.rs @@ -248,6 +248,30 @@ impl Write for Sink { } } +#[stable(feature = "write_mt", since = "1.47.0")] +impl Write for &Sink { + #[inline] + fn write(&mut self, buf: &[u8]) -> io::Result { + Ok(buf.len()) + } + + #[inline] + fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { + let total_len = bufs.iter().map(|b| b.len()).sum(); + Ok(total_len) + } + + #[inline] + fn is_write_vectored(&self) -> bool { + true + } + + #[inline] + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + #[stable(feature = "std_debug", since = "1.16.0")] impl fmt::Debug for Sink { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { diff --git a/library/std/src/process.rs b/library/std/src/process.rs index c42bc1096528b..36521fdaec00f 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -262,6 +262,25 @@ impl Write for ChildStdin { } } +#[stable(feature = "write_mt", since = "1.47.0")] +impl Write for &ChildStdin { + fn write(&mut self, buf: &[u8]) -> io::Result { + self.inner.write(buf) + } + + fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { + self.inner.write_vectored(bufs) + } + + fn is_write_vectored(&self) -> bool { + self.inner.is_write_vectored() + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + impl AsInner for ChildStdin { fn as_inner(&self) -> &AnonPipe { &self.inner From ec7f9b927f1896c7f29c602d6b0f961c891d0941 Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Fri, 11 Sep 2020 11:11:34 +0200 Subject: [PATCH 2/4] Deduplicates io::Write implementations --- library/std/src/io/stdio.rs | 28 ++++++++++++++-------------- library/std/src/process.rs | 8 ++++---- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index f320cf907c365..cccc0aadf9989 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -584,26 +584,26 @@ impl fmt::Debug for Stdout { #[stable(feature = "rust1", since = "1.0.0")] impl Write for Stdout { fn write(&mut self, buf: &[u8]) -> io::Result { - self.lock().write(buf) + (&*self).write(buf) } fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { - self.lock().write_vectored(bufs) + (&*self).write_vectored(bufs) } #[inline] fn is_write_vectored(&self) -> bool { - self.lock().is_write_vectored() + io::Write::is_write_vectored(&&*self) } fn flush(&mut self) -> io::Result<()> { - self.lock().flush() + (&*self).flush() } fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { - self.lock().write_all(buf) + (&*self).write_all(buf) } fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> { - self.lock().write_all_vectored(bufs) + (&*self).write_all_vectored(bufs) } fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> { - self.lock().write_fmt(args) + (&*self).write_fmt(args) } } @@ -787,26 +787,26 @@ impl fmt::Debug for Stderr { #[stable(feature = "rust1", since = "1.0.0")] impl Write for Stderr { fn write(&mut self, buf: &[u8]) -> io::Result { - self.lock().write(buf) + (&*self).write(buf) } fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { - self.lock().write_vectored(bufs) + (&*self).write_vectored(bufs) } #[inline] fn is_write_vectored(&self) -> bool { - self.lock().is_write_vectored() + io::Write::is_write_vectored(&&*self) } fn flush(&mut self) -> io::Result<()> { - self.lock().flush() + (&*self).flush() } fn write_all(&mut self, buf: &[u8]) -> io::Result<()> { - self.lock().write_all(buf) + (&*self).write_all(buf) } fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> { - self.lock().write_all_vectored(bufs) + (&*self).write_all_vectored(bufs) } fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> io::Result<()> { - self.lock().write_fmt(args) + (&*self).write_fmt(args) } } diff --git a/library/std/src/process.rs b/library/std/src/process.rs index 36521fdaec00f..d620a720be3ab 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -246,19 +246,19 @@ pub struct ChildStdin { #[stable(feature = "process", since = "1.0.0")] impl Write for ChildStdin { fn write(&mut self, buf: &[u8]) -> io::Result { - self.inner.write(buf) + (&*self).write(buf) } fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> io::Result { - self.inner.write_vectored(bufs) + (&*self).write_vectored(bufs) } fn is_write_vectored(&self) -> bool { - self.inner.is_write_vectored() + io::Write::is_write_vectored(&&*self) } fn flush(&mut self) -> io::Result<()> { - Ok(()) + (&*self).flush() } } From 88a29e630c1ce05fa28ec927e26cbf8a19d6efc4 Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Mon, 21 Sep 2020 08:52:59 +0200 Subject: [PATCH 3/4] Updates stability attributes to the current nightly version --- library/std/src/io/stdio.rs | 4 ++-- library/std/src/io/util.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/library/std/src/io/stdio.rs b/library/std/src/io/stdio.rs index cccc0aadf9989..b1033636a4536 100644 --- a/library/std/src/io/stdio.rs +++ b/library/std/src/io/stdio.rs @@ -607,7 +607,7 @@ impl Write for Stdout { } } -#[stable(feature = "write_mt", since = "1.47.0")] +#[stable(feature = "write_mt", since = "1.48.0")] impl Write for &Stdout { fn write(&mut self, buf: &[u8]) -> io::Result { self.lock().write(buf) @@ -810,7 +810,7 @@ impl Write for Stderr { } } -#[stable(feature = "write_mt", since = "1.47.0")] +#[stable(feature = "write_mt", since = "1.48.0")] impl Write for &Stderr { fn write(&mut self, buf: &[u8]) -> io::Result { self.lock().write(buf) diff --git a/library/std/src/io/util.rs b/library/std/src/io/util.rs index ce4dd1dcd493d..482378a6aba25 100644 --- a/library/std/src/io/util.rs +++ b/library/std/src/io/util.rs @@ -248,7 +248,7 @@ impl Write for Sink { } } -#[stable(feature = "write_mt", since = "1.47.0")] +#[stable(feature = "write_mt", since = "1.48.0")] impl Write for &Sink { #[inline] fn write(&mut self, buf: &[u8]) -> io::Result { From 0acb0ed1841d362a1119ba2ad293683660bdfaaf Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Mon, 21 Sep 2020 08:12:40 +0100 Subject: [PATCH 4/4] Update library/std/src/process.rs Co-authored-by: David Tolnay --- library/std/src/process.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/std/src/process.rs b/library/std/src/process.rs index d620a720be3ab..b2815ba32c4f4 100644 --- a/library/std/src/process.rs +++ b/library/std/src/process.rs @@ -262,7 +262,7 @@ impl Write for ChildStdin { } } -#[stable(feature = "write_mt", since = "1.47.0")] +#[stable(feature = "write_mt", since = "1.48.0")] impl Write for &ChildStdin { fn write(&mut self, buf: &[u8]) -> io::Result { self.inner.write(buf)