Skip to content

Make BorrowedBuf and BorrowedCursor generic over the data#149749

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

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

Conversation

@joshtriplett

@joshtriplett joshtriplett commented Dec 7, 2025

Copy link
Copy Markdown
Member

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

rustbot commented Dec 7, 2025

Copy link
Copy Markdown
Collaborator

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

@Amanieu Amanieu left a comment

Copy link
Copy Markdown
Member

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

bors commented Dec 17, 2025

Copy link
Copy Markdown
Collaborator

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

@scottmcm scottmcm left a comment

Copy link
Copy Markdown
Member

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 Outdated
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

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

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
Now that the `impl` block has a bound, the one on `append` became
redundant.
@joshtriplett

Copy link
Copy Markdown
Member Author

Pushing one additional commit with a minor fix (no semantic effect).

Caught by an experiment trying PR review with an AI; used for review only, no code. It noted seven issues, of which 1 was real (this one), 1 was obviously wrong (suggesting a test that would have called set_init with the buffer still uninitialized), 1 was subtly wrong (it would have provided a safe conjure_zst), and 4 were quibbles about code refactoring in diff context that this PR doesn't change.

@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/ O-windows Operating system: Windows labels Jun 2, 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 Jun 2, 2026
@joboet

joboet commented Jun 11, 2026

Copy link
Copy Markdown
Member

Looks fine!
@bors r+ rollup

@rust-bors

rust-bors Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 5948b47 has been approved by joboet

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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