Skip to content

std::io::Take: Clarify & optimize BorrowedBuf::set_init usage.#155317

Merged
rust-bors[bot] merged 4 commits intorust-lang:mainfrom
briansmith:b/take-opt
Apr 27, 2026
Merged

std::io::Take: Clarify & optimize BorrowedBuf::set_init usage.#155317
rust-bors[bot] merged 4 commits intorust-lang:mainfrom
briansmith:b/take-opt

Conversation

@briansmith
Copy link
Copy Markdown
Contributor

@briansmith briansmith commented Apr 14, 2026

Don't initialize buf if it was already initialized. Clarify safety comments.

Move the buf.advance() call to make the initialization more like
calling buf.ensure_init(), then clarify how the code here is an
optimized variant of ensure_init.

@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 Apr 14, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 14, 2026

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: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 8 candidates

@briansmith briansmith force-pushed the b/take-opt branch 2 times, most recently from ec92cd6 to 3c478cb Compare April 14, 2026 22:20
Copy link
Copy Markdown
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

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

Mostly questions at this point, not necessarily blockers for merging. Thanks!

View changes since this review

Comment thread library/std/src/io/mod.rs Outdated
unsafe {
// SAFETY: filled bytes have been filled
buf.advance(filled);
}
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.

Doesn't moving this up cause limit to be wrong when we decide what to initialize below? It would need to be limit - filled now, unless I'm missing something?

Either way I think a comment would be good noting whether order matters (or not), and if my suspicion of breakage is right is accurate then we probably should try to come up with a test...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Doesn't moving this up cause limit to be wrong when we decide what to initialize below? It would need to be limit - filled now, unless I'm missing something?

Yes, you are right. Thanks for catching this. I decided to remove the comment referencing ensure_init and I moved the advance call back below.

Either way I think a comment would be good noting whether order matters (or not),

I created a local variable named unfilled_before_advance to clarify this.

and if my suspicion of breakage is right is accurate then we probably should try to come up with a test...

I would like to write a test for this but I am not sure I will have time soon.

Comment thread library/std/src/io/mod.rs
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 18, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 18, 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 Apr 18, 2026
@rustbot

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 22, 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.

@briansmith
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@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 Apr 22, 2026
Copy link
Copy Markdown
Member

@Mark-Simulacrum Mark-Simulacrum left a comment

Choose a reason for hiding this comment

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

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 26, 2026

📌 Commit 3a0a14f has been approved by Mark-Simulacrum

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 Apr 26, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 26, 2026
…lacrum

`std::io::Take`: Clarify & optimize `BorrowedBuf::set_init` usage.

Don't initialize `buf` if it was already initialized. Clarify safety comments.

Move the `buf.advance()` call to make the initialization more like
calling `buf.ensure_init()`, then clarify how the code here is an
optimized variant of `ensure_init`.
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Apr 26, 2026
…lacrum

`std::io::Take`: Clarify & optimize `BorrowedBuf::set_init` usage.

Don't initialize `buf` if it was already initialized. Clarify safety comments.

Move the `buf.advance()` call to make the initialization more like
calling `buf.ensure_init()`, then clarify how the code here is an
optimized variant of `ensure_init`.
rust-bors Bot pushed a commit that referenced this pull request Apr 26, 2026
…uwer

Rollup of 9 pull requests

Successful merges:

 - #149624 (Fix requires_lto targets needing lto set in cargo)
 - #152443 (NVPTX: Drop support for old architectures and old ISAs)
 - #155317 (`std::io::Take`: Clarify & optimize `BorrowedBuf::set_init` usage.)
 - #155588 (Implement more traits for FRTs)
 - #155682 (Add boxing suggestions for `impl Trait` return type mismatches)
 - #155770 (Avoid misleading closure return type note)
 - #155818 (Convert attribute `FinalizeFn` to fn pointer)
 - #155829 (rustc_attr_parsing: use a `try {}` in `or_malformed`)
 - #155835 (couple of `crate_name` cleanups)
rust-bors Bot pushed a commit that referenced this pull request Apr 27, 2026
Rollup of 12 pull requests

Successful merges:

 - #149624 (Fix requires_lto targets needing lto set in cargo)
 - #155317 (`std::io::Take`: Clarify & optimize `BorrowedBuf::set_init` usage.)
 - #155579 (Make Rcs and Arcs use pointer comparison for unsized types)
 - #155588 (Implement more traits for FRTs)
 - #155708 (Fix heap overflow in slice::join caused by misbehaving Borrow)
 - #155778 (Avoid Vec allocation in TyCtxt::mk_place_elem)
 - #151014 (std: sys: process: uefi: Add program searching)
 - #155682 (Add boxing suggestions for `impl Trait` return type mismatches)
 - #155770 (Avoid misleading closure return type note)
 - #155818 (Convert attribute `FinalizeFn` to fn pointer)
 - #155829 (rustc_attr_parsing: use a `try {}` in `or_malformed`)
 - #155835 (couple of `crate_name` cleanups)
@rust-bors rust-bors Bot merged commit 12a843a into rust-lang:main Apr 27, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 27, 2026
rust-timer added a commit that referenced this pull request Apr 27, 2026
Rollup merge of #155317 - briansmith:b/take-opt, r=Mark-Simulacrum

`std::io::Take`: Clarify & optimize `BorrowedBuf::set_init` usage.

Don't initialize `buf` if it was already initialized. Clarify safety comments.

Move the `buf.advance()` call to make the initialization more like
calling `buf.ensure_init()`, then clarify how the code here is an
optimized variant of `ensure_init`.
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants