Skip to content
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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

thaliaarchi
Copy link
Contributor

@thaliaarchi thaliaarchi commented Feb 9, 2025

The raw types for standard I/O across platforms vary by which of the optional io::Read and io::Write methods they provide and most just use the defaults. Only Unix implements read_buf, read_vectored, and write_vectored for its stdio. Provide these methods for the platforms which support them or when useful. Additionally, provide missing methods for StdinRaw, StdoutLock, and StderrLock, 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.

x check library/std --target=x86_64-unknown-hermit
x check library/std --target=aarch64-unknown-linux-gnu
x check library/std --target=aarch64-apple-darwin
x check library/std --target=x86_64-fortanix-unknown-sgx
x check library/std --target=aarch64-kmc-solid_asp3
x check library/std --target=aarch64-unknown-teeos
x check library/std --target=x86_64-unknown-uefi
x check library/std --target=wasm32-unknown-unknown
x check library/std --target=wasm32-wasip1
x check library/std --target=x86_64-pc-windows-gnu
x check library/std --target=riscv32imac-unknown-xous-elf
x check library/std --target=riscv32im-risc0-zkvm-elf

Open questions

I have been unable to find sufficient documentation or a libc for these platforms. Do they support these features?

  • TEEOS: vectored write
  • Xous: vectored write
  • zkvm: vectored read/write

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, and write_fmt.

Further work

  • Windows and UEFI stdio use UTF-16, so will take more work to flesh out. I plan to refactor the WTF-8 abstraction from OsStr to accommodate this.

Progress on optional Read/Write methods is tracked in #136756.

@rustbot
Copy link
Collaborator

rustbot commented Feb 9, 2025

r? @Noratrieb

rustbot has assigned @Noratrieb.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added O-hermit Operating System: Hermit O-SGX Target: SGX O-solid Operating System: SOLID O-wasi Operating system: Wasi, Webassembly System Interface S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 9, 2025
@rust-log-analyzer

This comment has been minimized.

Comment on lines 124 to 129
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
handle_ebadf(self.0.read_exact(buf), ())
}
Copy link
Member

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.

Copy link
Contributor Author

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.

@@ -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)
Copy link
Member

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.

Copy link
Contributor Author

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.

library/std/src/io/stdio.rs Outdated Show resolved Hide resolved
library/std/src/io/stdio.rs Outdated Show resolved Hide resolved
@thaliaarchi thaliaarchi force-pushed the io-optional-methods/stdio branch from 6d101c2 to 3deb1ce Compare February 9, 2025 23:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-hermit Operating System: Hermit O-SGX Target: SGX O-solid Operating System: SOLID O-wasi Operating system: Wasi, Webassembly System Interface S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants