Skip to content

Implement Write for Cursor<W: AsMut<[u8]>> - #160960

Open
bushrat011899 wants to merge 1 commit into
rust-lang:mainfrom
bushrat011899:core_io_cursor_write_rewrite
Open

Implement Write for Cursor<W: AsMut<[u8]>>#160960
bushrat011899 wants to merge 1 commit into
rust-lang:mainfrom
bushrat011899:core_io_cursor_write_rewrite

Conversation

@bushrat011899

@bushrat011899 bushrat011899 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Change WriteThroughCursor into a specialization which allows Vec and &mut Vec to extend their allocation for writing. This makes discoverability of the Write implementation better.

ACP: rust-lang/libs-team#853
Tracking Issue: #154046
Follow Up To: #160952 & #158537

Description

Currently, Write is implemented for Cursor<W>, where W is one of:

  • &mut [u8]
  • for<const N: usize> [u8; N]
  • for<A: Allocator> Box<[u8], A>
  • for<A: Allocator> Vec<u8, A>
  • for<A: Allocator> &mut Vec<u8, A>

When moving Write into core::io, it was noted here that the documentation of Cursor would be degraded due to the introduction of WriteThroughCursor, an indirection trait to allow Cursor and Write in core, while Box and Vec exist in alloc.

To resolve this documentation regression, and add additional functionality as well, I'm proposing we instead implement Write for Cursor<W> where W: AsMut<[u8]>. All 5 of the original types above implement AsMut<[u8]>, so we'll correctly document the breadth of support.

However, for Vec and &mut Vec, I use specialization to allow their implementation to extend, rather than only writing to the existing slice. While this still uses an indirection trait (SpecCursorWrite), but now this is purely for specialization, and therefore doesn't need to be documented publicly.

I'm opening a PR directly as a reference point, but I suspect this will need an ACP since this changes the public API.


Notes

  • No AI tooling of any kind was used during the creation of this PR.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 12, 2026
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Aug 12, 2026
@rustbot

rustbot commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
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 12 candidates
  • Random selection from JohnTitor, Mark-Simulacrum, clarfonthey, nia-e

@rust-bors

This comment has been minimized.

Change `WriteThroughCursor` into a specialization which allows `Vec` and `&mut Vec` to extend their allocation for writing. This makes discoverability of the `Write` implementation better.
@bushrat011899
bushrat011899 force-pushed the core_io_cursor_write_rewrite branch from 517f9a5 to 8142083 Compare August 12, 2026 22:07
@rustbot

rustbot commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

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.

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

Labels

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.

3 participants