Skip to content

Make BorrowedBuf and BorrowedCursor generic over the data#149749

Open
joshtriplett wants to merge 6 commits into
rust-lang:mainfrom
joshtriplett:borrowed-buf-generic
Open

Make BorrowedBuf and BorrowedCursor generic over the data#149749
joshtriplett wants to merge 6 commits into
rust-lang:mainfrom
joshtriplett:borrowed-buf-generic

Conversation

@joshtriplett
Copy link
Copy Markdown
Member

@joshtriplett joshtriplett commented Dec 7, 2025

View all comments

Replace uses of u8 with a generic type. This will allow reusing
BorrowedBuf elsewhere in the standard library, for purposes other than
byte buffers.

This currently requires T: Copy, to prevent T: Drop, which would require additional careful handling. We can consider relaxing that restriction in the future if we make T: Drop work (without pessimizing Copy types like u8).

ensure_init currently continues to require u8. We could potentially generalize it to T: Default, but we'd need to ensure it produced the same or better assembly for u8.


As discussed in libs-api; cc @Amanieu.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 7, 2025
@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label Dec 7, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Dec 7, 2025

r? @scottmcm

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

@joshtriplett joshtriplett added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Dec 7, 2025
Copy link
Copy Markdown
Member

@Amanieu Amanieu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me for the rest

View changes since this review

Comment thread library/core/src/io/borrowed_buf.rs Outdated
Comment thread library/core/src/io/borrowed_buf.rs Outdated
@bors
Copy link
Copy Markdown
Collaborator

bors commented Dec 17, 2025

☔ The latest upstream changes (presumably #150106) made this pull request unmergeable. Please resolve the merge conflicts.

Copy link
Copy Markdown
Member

@scottmcm scottmcm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be fine to land this PR for generic-izing the type, then we can land another PR right afterwards to remove the default and update the uses.

Code looks good to me, and I tried to scan for anything making unsaid assumptions about T (being Copy, Freeze, etc) and didn't find any that weren't already updated.

So r=me with conflicts fixed.


Aside: excited to try this out with generic types. Might be a great way to solve some of the Iterator::next_chunk or Vec::push_within_existing_capacity scenarios.

View changes since this review

Comment thread library/core/src/io/borrowed_buf.rs
@scottmcm scottmcm added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 9, 2026
@joshtriplett
Copy link
Copy Markdown
Member Author

I'm going to wait to update this until after #150129 is merged.

Comment thread library/core/src/io/borrowed_buf.rs Outdated
Comment thread library/core/src/io/borrowed_buf.rs Outdated
Comment thread library/core/src/io/borrowed_buf.rs Outdated
Comment thread library/core/src/io/borrowed_buf.rs Outdated
Comment thread library/core/src/io/borrowed_buf.rs
Comment thread library/core/src/io/borrowed_buf.rs Outdated
Comment thread library/core/src/io/borrowed_buf.rs Outdated
@scottmcm scottmcm removed their assignment May 18, 2026
Replace uses of `u8` with a generic type. This will allow reusing
`BorrowedBuf` elsewhere in the standard library, for purposes other than
byte buffers.

This currently requires `T: Copy`, to prevent `T: Drop`, which would
require additional careful handling. We can consider relaxing that
restriction in the future if we make `T: Drop` work (without pessimizing
`Copy` types like `u8`).

`ensure_init` currently continues to require `u8`. We could potentially
generalize it to `T: Default`, but we'd need to ensure it produced the
same or better assembly for `u8`.
@joshtriplett joshtriplett force-pushed the borrowed-buf-generic branch from c150931 to f7b1581 Compare May 25, 2026 12:19
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 25, 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.

Comment thread library/core/src/io/borrowed_buf.rs Outdated
Comment thread library/core/src/io/borrowed_buf.rs Outdated
Comment thread library/core/src/io/borrowed_buf.rs Outdated
This default simplified the commit making these types generic, but we
don't actually want the default. Rewrite every user to specify `u8`
explicitly.
@rustbot rustbot added O-SGX Target: SGX O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface O-wasm Target: WASM (WebAssembly), http://webassembly.org/ labels May 30, 2026
@rustbot rustbot added the O-windows Operating system: Windows label May 30, 2026
@joshtriplett joshtriplett removed O-windows Operating system: Windows O-wasm Target: WASM (WebAssembly), http://webassembly.org/ O-SGX Target: SGX O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface labels May 30, 2026
@joshtriplett
Copy link
Copy Markdown
Member Author

@rustbot ready

This is ready for review.

I would suggest looking at it commit by commit, as the change to remove the default T = u8 is very large and noisy, but largely mechanical.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 30, 2026
@joshtriplett
Copy link
Copy Markdown
Member Author

r? libs

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label May 30, 2026
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. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants