-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Provide optional Read
/Write
method for stdio
#136769
base: master
Are you sure you want to change the base?
Provide optional Read
/Write
method for stdio
#136769
Conversation
r? @Noratrieb rustbot has assigned @Noratrieb. Use |
This comment has been minimized.
This comment has been minimized.
library/std/src/io/stdio.rs
Outdated
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> { | ||
handle_ebadf(self.0.read_exact(buf), ()) | ||
} |
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.
This is incorrect, this should return UnexpectedEof
instead (which the default implementation does), as the buffer hasn't been filled.
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.
Ah, good catch. I missed that ebadf is interpreted differently for reading vs writing. Should be fixed now.
library/std/src/io/stdio.rs
Outdated
@@ -820,6 +828,9 @@ impl Write for StdoutLock<'_> { | |||
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> io::Result<()> { | |||
self.inner.borrow_mut().write_all_vectored(bufs) | |||
} | |||
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> io::Result<()> { | |||
self.inner.borrow_mut().write_fmt(fmt) |
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.
We don't do this because the formatting implementations might try to use Stdout
themselves, which would lead to panicking.
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.
Thanks. I had overlooked that this could call user code, and was only thinking about locking concerns on the stdlib side. I dropped that commit now.
6d101c2
to
3deb1ce
Compare
The raw types for standard I/O across platforms vary by which of the optional
io::Read
andio::Write
methods they provide and most just use the defaults. Only Unix implementsread_buf
,read_vectored
, andwrite_vectored
for its stdio. Provide these methods for the platforms which support them or when useful. Additionally, provide missing methods forStdinRaw
,StdoutLock
, andStderrLock
, which the other similar types provide.I have checked the code against a target per platform modified, with wasm32-unknown-unknown for unsupported stdio, as well as Linux, macOS, and Windows.
Open questions
I have been unable to find sufficient documentation or a libc for these platforms. Do they support these features?
Would any of the other methods be useful to provide for platform stdio types? Those are:
read_buf_exact
,read_exact
,read_to_end
,read_to_string
,write_all
,write_all_vectored
, andwrite_fmt
.Further work
OsStr
to accommodate this.Progress on optional
Read
/Write
methods is tracked in #136756.