Skip to content

Move OS-dependent parts of std::io to alloc#152918

Open
a1phyr wants to merge 3 commits intorust-lang:mainfrom
a1phyr:alloc_io
Open

Move OS-dependent parts of std::io to alloc#152918
a1phyr wants to merge 3 commits intorust-lang:mainfrom
a1phyr:alloc_io

Conversation

@a1phyr
Copy link
Contributor

@a1phyr a1phyr commented Feb 20, 2026

This is the first (and the hardest) part of a plan to move most of std::io to alloc.

This PR has three commits:

  • The first enable alloc::ffi with no_rc and no_sync because they where annoying me.
  • The second moves io::Error to alloc.
    • Creating Error from raw OS errors is restricted to std via incoherent impls.
    • OS-dependent stuff needed by alloc is set at runtime by std when creating Error from raw OS errors (so no linking required, and not dependent of how Rust code is started). Can be moved to externally implementable one day.
  • The third moves IoSlice and IoSliceMut to alloc (required by Read and Write, exact layout depend on the OS).
    • Moves OS-dependent definition to alloc, but do not link to anything either.

I expect the introduction of OS-dependent code to alloc quite controversial, but please note that this PR does not expose anything OS-dependent to alloc users.

Also this PR abuses the fact that tidy does not warn on cfg_select! in alloc like it does for #[cfg()] 😈

The rest of std::io that can move should do so easily (obviously not things like stdio, IsTerminal, pipes, etc), except maybe for io::copy which has OS-dependent specialization. If this get merged, I will make a follup-up PR with all all this. In the meantime, a PR containing only the hard parts will avoid perpetual conflicts and rebases.

Some questions

Why not core?

Well this could be tried in the future. The main issue is that Read directly uses Vec.

Why not new Read/Write traits?

Why about not having them and make the existing one work for everyone?

Previous discussion (far larger than these few links)

Cc #48331
Prior try to move Error to core (not alloc): #116685
Prior discussion on this specific plan: https://internals.rust-lang.org/t/a-plan-to-move-most-of-std-io-to-alloc/23678/9
Many #![no_std] crates reimplementing std::io:

@rustbot rustbot added 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 20, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 20, 2026

r? @jhpratt

rustbot has assigned @jhpratt.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 7 candidates
  • Random selection from Mark-Simulacrum, jhpratt

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@lygstate
Copy link
Contributor

lygstate commented Feb 21, 2026

I have a idea, that move iovec into core::ffi first, and it's just different layout on UEFI/Windows/Other, for unknow or other triple, use

struct iovec {
    iov_base: *mut c_void,
    iov_len: usize,
}

for windows/UEFI, use

struct iovec {
    iov_len: c_ulong,
    iov_base: *mut c_void,
}

and for RawOsError, it's can also defined in core:ffi as

os_errno_t

@a1phyr
Copy link
Contributor Author

a1phyr commented Feb 21, 2026

It would possible indeed. I think that what we'll do will depend on T-libs team preference.

@rustbot
Copy link
Collaborator

rustbot commented Feb 21, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added the T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. label Feb 21, 2026
@rust-log-analyzer

This comment has been minimized.

@a1phyr
Copy link
Contributor Author

a1phyr commented Feb 21, 2026

To make rustdoc happy, I had to remove a bunch a links in io documentation. Most of them will be able to be added back when the rest of io arrives. The other will need a closer look to the incoherent impls weirdness.

@jhpratt
Copy link
Member

jhpratt commented Mar 1, 2026

@rustbot reroll

@rustbot rustbot assigned joboet and unassigned jhpratt Mar 1, 2026
@tgross35
Copy link
Contributor

tgross35 commented Mar 2, 2026

This is the first (and the hardest) part of a plan to move most of std::io to alloc.

A specific plan should be proposed as an ACP before starting too much work, there are tradeoffs and alternatives that need to be discussed and accepted by the libs-api team.

@rustbot author

@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 2, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 2, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 2, 2026
@tgross35 tgross35 added the needs-acp This change is blocked on the author creating an ACP. label Mar 2, 2026
@lygstate lygstate mentioned this pull request Mar 2, 2026
4 tasks
@kayabaNerve
Copy link

As someone who maintains my own set of no-std io traits (multiple, actually), I'm very hopeful about this.

However, as potentially justifying to the prior call for discussion before moving forward, I'm unclear on the role of OS-specific code in alloc even if not exposed. Specifically, what would happen when a no-std build targets a nonexistent or unknown operating system? Part of my interest in no-std is for explicitly cases where there is a way to allocate memory, and a defined executable format, but no other assumptions about the environment are made (letting the developer then use something like libc instead of std).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-acp This change is blocked on the author creating an ACP. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants