-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Fixup stout/stderr on Windows #32184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,6 @@ use prelude::v1::*; | |
use io::prelude::*; | ||
|
||
use cell::{RefCell, BorrowState}; | ||
use cmp; | ||
use fmt; | ||
use io::lazy::Lazy; | ||
use io::{self, BufReader, LineWriter}; | ||
|
@@ -312,22 +311,6 @@ impl<'a> BufRead for StdinLock<'a> { | |
fn consume(&mut self, n: usize) { self.inner.consume(n) } | ||
} | ||
|
||
// As with stdin on windows, stdout often can't handle writes of large | ||
// sizes. For an example, see #14940. For this reason, don't try to | ||
// write the entire output buffer on windows. On unix we can just | ||
// write the whole buffer all at once. | ||
// | ||
// For some other references, it appears that this problem has been | ||
// encountered by others [1] [2]. We choose the number 8KB just because | ||
// libuv does the same. | ||
// | ||
// [1]: https://tahoe-lafs.org/trac/tahoe-lafs/ticket/1232 | ||
// [2]: http://www.mail-archive.com/[email protected]/msg00661.html | ||
#[cfg(windows)] | ||
const OUT_MAX: usize = 8192; | ||
#[cfg(unix)] | ||
const OUT_MAX: usize = ::usize::MAX; | ||
|
||
/// A handle to the global standard output stream of the current process. | ||
/// | ||
/// Each handle shares a global buffer of data to be written to the standard | ||
|
@@ -440,7 +423,7 @@ impl Write for Stdout { | |
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<'a> Write for StdoutLock<'a> { | ||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
self.inner.borrow_mut().write(&buf[..cmp::min(buf.len(), OUT_MAX)]) | ||
self.inner.borrow_mut().write(buf) | ||
} | ||
fn flush(&mut self) -> io::Result<()> { | ||
self.inner.borrow_mut().flush() | ||
|
@@ -546,7 +529,7 @@ impl Write for Stderr { | |
#[stable(feature = "rust1", since = "1.0.0")] | ||
impl<'a> Write for StderrLock<'a> { | ||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { | ||
self.inner.borrow_mut().write(&buf[..cmp::min(buf.len(), OUT_MAX)]) | ||
self.inner.borrow_mut().write(buf) | ||
} | ||
fn flush(&mut self) -> io::Result<()> { | ||
self.inner.borrow_mut().flush() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,8 +58,28 @@ fn write(out: &Output, data: &[u8]) -> io::Result<usize> { | |
Output::Console(ref c) => c.get().raw(), | ||
Output::Pipe(ref p) => return p.get().write(data), | ||
}; | ||
let utf16 = match str::from_utf8(data).ok() { | ||
Some(utf8) => utf8.encode_utf16().collect::<Vec<u16>>(), | ||
// As with stdin on windows, stdout often can't handle writes of large | ||
// sizes. For an example, see #14940. For this reason, don't try to | ||
// write the entire output buffer on windows. | ||
// | ||
// For some other references, it appears that this problem has been | ||
// encountered by others [1] [2]. We choose the number 8K just because | ||
// libuv does the same. | ||
// | ||
// [1]: https://tahoe-lafs.org/trac/tahoe-lafs/ticket/1232 | ||
// [2]: http://www.mail-archive.com/[email protected]/msg00661.html | ||
const OUT_MAX: usize = 8192; | ||
let (utf16, data_len) = match str::from_utf8(data).ok() { | ||
Some(mut utf8) => { | ||
if utf8.len() > OUT_MAX { | ||
let mut new_len = OUT_MAX; | ||
while !utf8.is_char_boundary(new_len) { | ||
new_len -= 1; | ||
} | ||
utf8 = &utf8[..new_len]; | ||
} | ||
(utf8.encode_utf16().collect::<Vec<u16>>(), utf8.len()) | ||
} | ||
None => return Err(invalid_encoding()), | ||
}; | ||
let mut written = 0; | ||
|
@@ -74,7 +94,7 @@ fn write(out: &Output, data: &[u8]) -> io::Result<usize> { | |
// FIXME if this only partially writes the utf16 buffer then we need to | ||
// figure out how many bytes of `data` were actually written | ||
assert_eq!(written as usize, utf16.len()); | ||
Ok(data.len()) | ||
Ok(data_len) | ||
} | ||
|
||
impl Stdin { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this could do something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the sense that it would: