From 27c8dfddac4c69a6fd399abe537e1007306c58cf Mon Sep 17 00:00:00 2001 From: Austin Bonander Date: Sun, 3 Feb 2019 22:38:43 -0800 Subject: [PATCH] Improve error message and docs for non-UTF-8 bytes in stdio on Windows cc #23344 --- src/libstd/io/stdio.rs | 45 +++++++++++++++++++++++++++++++++ src/libstd/sys/windows/stdio.rs | 4 ++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index d249469323063..4068c0f9c7de5 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -131,6 +131,11 @@ fn handle_ebadf(r: io::Result, default: T) -> io::Result { /// /// [`io::stdin`]: fn.stdin.html /// [`BufRead`]: trait.BufRead.html +/// +/// ### Note: Windows Portability Consideration +/// When operating in a console, the Windows implementation of this stream does not support +/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return +/// an error. #[stable(feature = "rust1", since = "1.0.0")] pub struct Stdin { inner: Arc>>>, @@ -144,6 +149,11 @@ pub struct Stdin { /// [`Read`]: trait.Read.html /// [`BufRead`]: trait.BufRead.html /// [`Stdin::lock`]: struct.Stdin.html#method.lock +/// +/// ### Note: Windows Portability Consideration +/// When operating in a console, the Windows implementation of this stream does not support +/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return +/// an error. #[stable(feature = "rust1", since = "1.0.0")] pub struct StdinLock<'a> { inner: MutexGuard<'a, BufReader>>, @@ -157,6 +167,11 @@ pub struct StdinLock<'a> { /// /// [lock]: struct.Stdin.html#method.lock /// +/// ### Note: Windows Portability Consideration +/// When operating in a console, the Windows implementation of this stream does not support +/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return +/// an error. +/// /// # Examples /// /// Using implicit synchronization: @@ -328,6 +343,11 @@ impl<'a> fmt::Debug for StdinLock<'a> { /// /// Created by the [`io::stdout`] method. /// +/// ### Note: Windows Portability Consideration +/// When operating in a console, the Windows implementation of this stream does not support +/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return +/// an error. +/// /// [`lock`]: #method.lock /// [`io::stdout`]: fn.stdout.html #[stable(feature = "rust1", since = "1.0.0")] @@ -343,6 +363,11 @@ pub struct Stdout { /// This handle implements the [`Write`] trait, and is constructed via /// the [`Stdout::lock`] method. /// +/// ### Note: Windows Portability Consideration +/// When operating in a console, the Windows implementation of this stream does not support +/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return +/// an error. +/// /// [`Write`]: trait.Write.html /// [`Stdout::lock`]: struct.Stdout.html#method.lock #[stable(feature = "rust1", since = "1.0.0")] @@ -358,6 +383,11 @@ pub struct StdoutLock<'a> { /// /// [Stdout::lock]: struct.Stdout.html#method.lock /// +/// ### Note: Windows Portability Consideration +/// When operating in a console, the Windows implementation of this stream does not support +/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return +/// an error. +/// /// # Examples /// /// Using implicit synchronization: @@ -476,6 +506,11 @@ impl<'a> fmt::Debug for StdoutLock<'a> { /// For more information, see the [`io::stderr`] method. /// /// [`io::stderr`]: fn.stderr.html +/// +/// ### Note: Windows Portability Consideration +/// When operating in a console, the Windows implementation of this stream does not support +/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return +/// an error. #[stable(feature = "rust1", since = "1.0.0")] pub struct Stderr { inner: Arc>>>, @@ -487,6 +522,11 @@ pub struct Stderr { /// the [`Stderr::lock`] method. /// /// [`Stderr::lock`]: struct.Stderr.html#method.lock +/// +/// ### Note: Windows Portability Consideration +/// When operating in a console, the Windows implementation of this stream does not support +/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return +/// an error. #[stable(feature = "rust1", since = "1.0.0")] pub struct StderrLock<'a> { inner: ReentrantMutexGuard<'a, RefCell>>, @@ -496,6 +536,11 @@ pub struct StderrLock<'a> { /// /// This handle is not buffered. /// +/// ### Note: Windows Portability Consideration +/// When operating in a console, the Windows implementation of this stream does not support +/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return +/// an error. +/// /// # Examples /// /// Using implicit synchronization: diff --git a/src/libstd/sys/windows/stdio.rs b/src/libstd/sys/windows/stdio.rs index a4f4bd22cd921..0ea19a855257b 100644 --- a/src/libstd/sys/windows/stdio.rs +++ b/src/libstd/sys/windows/stdio.rs @@ -188,7 +188,9 @@ impl Output { } fn invalid_encoding() -> io::Error { - io::Error::new(io::ErrorKind::InvalidData, "text was not valid unicode") + io::Error::new(io::ErrorKind::InvalidData, + "Windows stdio in console mode does not support non-UTF-8 byte sequences; \ + see https://github.com/rust-lang/rust/issues/23344") } fn readconsole_input_control(wakeup_mask: c::ULONG) -> c::CONSOLE_READCONSOLE_CONTROL {