Skip to content

Commit 27c8dfd

Browse files
committed
Improve error message and docs for non-UTF-8 bytes in stdio on Windows
cc rust-lang#23344
1 parent 4314dba commit 27c8dfd

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/libstd/io/stdio.rs

+45
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ fn handle_ebadf<T>(r: io::Result<T>, default: T) -> io::Result<T> {
131131
///
132132
/// [`io::stdin`]: fn.stdin.html
133133
/// [`BufRead`]: trait.BufRead.html
134+
///
135+
/// ### Note: Windows Portability Consideration
136+
/// When operating in a console, the Windows implementation of this stream does not support
137+
/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
138+
/// an error.
134139
#[stable(feature = "rust1", since = "1.0.0")]
135140
pub struct Stdin {
136141
inner: Arc<Mutex<BufReader<Maybe<StdinRaw>>>>,
@@ -144,6 +149,11 @@ pub struct Stdin {
144149
/// [`Read`]: trait.Read.html
145150
/// [`BufRead`]: trait.BufRead.html
146151
/// [`Stdin::lock`]: struct.Stdin.html#method.lock
152+
///
153+
/// ### Note: Windows Portability Consideration
154+
/// When operating in a console, the Windows implementation of this stream does not support
155+
/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
156+
/// an error.
147157
#[stable(feature = "rust1", since = "1.0.0")]
148158
pub struct StdinLock<'a> {
149159
inner: MutexGuard<'a, BufReader<Maybe<StdinRaw>>>,
@@ -157,6 +167,11 @@ pub struct StdinLock<'a> {
157167
///
158168
/// [lock]: struct.Stdin.html#method.lock
159169
///
170+
/// ### Note: Windows Portability Consideration
171+
/// When operating in a console, the Windows implementation of this stream does not support
172+
/// non-UTF-8 byte sequences. Attempting to read bytes that are not valid UTF-8 will return
173+
/// an error.
174+
///
160175
/// # Examples
161176
///
162177
/// Using implicit synchronization:
@@ -328,6 +343,11 @@ impl<'a> fmt::Debug for StdinLock<'a> {
328343
///
329344
/// Created by the [`io::stdout`] method.
330345
///
346+
/// ### Note: Windows Portability Consideration
347+
/// When operating in a console, the Windows implementation of this stream does not support
348+
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
349+
/// an error.
350+
///
331351
/// [`lock`]: #method.lock
332352
/// [`io::stdout`]: fn.stdout.html
333353
#[stable(feature = "rust1", since = "1.0.0")]
@@ -343,6 +363,11 @@ pub struct Stdout {
343363
/// This handle implements the [`Write`] trait, and is constructed via
344364
/// the [`Stdout::lock`] method.
345365
///
366+
/// ### Note: Windows Portability Consideration
367+
/// When operating in a console, the Windows implementation of this stream does not support
368+
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
369+
/// an error.
370+
///
346371
/// [`Write`]: trait.Write.html
347372
/// [`Stdout::lock`]: struct.Stdout.html#method.lock
348373
#[stable(feature = "rust1", since = "1.0.0")]
@@ -358,6 +383,11 @@ pub struct StdoutLock<'a> {
358383
///
359384
/// [Stdout::lock]: struct.Stdout.html#method.lock
360385
///
386+
/// ### Note: Windows Portability Consideration
387+
/// When operating in a console, the Windows implementation of this stream does not support
388+
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
389+
/// an error.
390+
///
361391
/// # Examples
362392
///
363393
/// Using implicit synchronization:
@@ -476,6 +506,11 @@ impl<'a> fmt::Debug for StdoutLock<'a> {
476506
/// For more information, see the [`io::stderr`] method.
477507
///
478508
/// [`io::stderr`]: fn.stderr.html
509+
///
510+
/// ### Note: Windows Portability Consideration
511+
/// When operating in a console, the Windows implementation of this stream does not support
512+
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
513+
/// an error.
479514
#[stable(feature = "rust1", since = "1.0.0")]
480515
pub struct Stderr {
481516
inner: Arc<ReentrantMutex<RefCell<Maybe<StderrRaw>>>>,
@@ -487,6 +522,11 @@ pub struct Stderr {
487522
/// the [`Stderr::lock`] method.
488523
///
489524
/// [`Stderr::lock`]: struct.Stderr.html#method.lock
525+
///
526+
/// ### Note: Windows Portability Consideration
527+
/// When operating in a console, the Windows implementation of this stream does not support
528+
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
529+
/// an error.
490530
#[stable(feature = "rust1", since = "1.0.0")]
491531
pub struct StderrLock<'a> {
492532
inner: ReentrantMutexGuard<'a, RefCell<Maybe<StderrRaw>>>,
@@ -496,6 +536,11 @@ pub struct StderrLock<'a> {
496536
///
497537
/// This handle is not buffered.
498538
///
539+
/// ### Note: Windows Portability Consideration
540+
/// When operating in a console, the Windows implementation of this stream does not support
541+
/// non-UTF-8 byte sequences. Attempting to write bytes that are not valid UTF-8 will return
542+
/// an error.
543+
///
499544
/// # Examples
500545
///
501546
/// Using implicit synchronization:

src/libstd/sys/windows/stdio.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,9 @@ impl Output {
188188
}
189189

190190
fn invalid_encoding() -> io::Error {
191-
io::Error::new(io::ErrorKind::InvalidData, "text was not valid unicode")
191+
io::Error::new(io::ErrorKind::InvalidData,
192+
"Windows stdio in console mode does not support non-UTF-8 byte sequences; \
193+
see https://github.com/rust-lang/rust/issues/23344")
192194
}
193195

194196
fn readconsole_input_control(wakeup_mask: c::ULONG) -> c::CONSOLE_READCONSOLE_CONTROL {

0 commit comments

Comments
 (0)