Skip to content

Use drop_guard in some places in {core,alloc,std} - #161702

Open
GrigorenkoPV wants to merge 1 commit into
rust-lang:mainfrom
GrigorenkoPV:drop-guard
Open

Use drop_guard in some places in {core,alloc,std}#161702
GrigorenkoPV wants to merge 1 commit into
rust-lang:mainfrom
GrigorenkoPV:drop-guard

Conversation

@GrigorenkoPV

@GrigorenkoPV GrigorenkoPV commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Didn't touch the places where manual impl Drops had #[inline] on their fn drop or where the guard type had other impls beside Drop and/or was named a lot.

No LLMs used, only pure human slop.

@rustbot rustbot added O-unix Operating system: Unix-like 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 Aug 24, 2026
@rustbot

rustbot commented Aug 24, 2026

Copy link
Copy Markdown
Collaborator

r? @clarfonthey

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

@clarfonthey

Copy link
Copy Markdown
Contributor

The way the code was moved around makes this a bit more difficult to review, but otherwise, it looks good to me. Just a limitation of how diffs work.

I think this is a strict improvement regardless of stabilisation. I don't think this should require more than a rebase from Yosh, so, it should be fine that this conflicts with the stabilisation PR.

@bors r+ rollup

Thank you!

@rust-bors

rust-bors Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

📌 Commit a5a9d5d has been approved by clarfonthey

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 Aug 25, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 25, 2026
…they

Use `drop_guard` in some places in {core,alloc,std}

- Tracking issue: rust-lang#144426
- Will conflict with rust-lang#161520
- rust-lang#161550 would also be cool occasionally

Didn't touch the places where manual `impl Drop`s had `#[inline]` on their `fn drop` or where the guard type had other `impl`s beside `Drop` and/or was named a lot.

No LLMs used, only pure human slop.
rust-bors Bot pushed a commit that referenced this pull request Aug 25, 2026
Rollup of 9 pull requests

Successful merges:

 - #160533 (dirfd dir operations (3/4))
 - #161294 (add crashtests [6/N])
 - #160392 (library: motor: bump moto-rt ABI ver to 17)
 - #161646 (interpret: fix after_local_read handling)
 - #160819 (Rework `div_ceil` for nonzero integers)
 - #161591 (run `classify-runtime-const` test for `f128`)
 - #161680 (renovate: add lockfile update warning)
 - #161702 (Use `drop_guard` in some places in {core,alloc,std})
 - #161709 (Stop rustfmt deleting attributes in fn params)
@JonathanBrouwer

Copy link
Copy Markdown
Member

💔 I suspect this PR failed tests as part of a rollup
@bors r-

After fixing the problem, consider running a try job for the failed job before re-approving.

Link to failure: #161715 (comment)

@rust-bors rust-bors Bot 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 25, 2026
@rust-bors

rust-bors Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

This pull request was unapproved.

This PR was contained in a rollup (#161715), which was unapproved.

View changes since this unapproval

@GrigorenkoPV

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 Aug 25, 2026
@yoshuawuyts

Copy link
Copy Markdown
Member

@GrigorenkoPV thank you for filing this PR; this is really cool to see and a very helpful reference for how DropGuard actually ends up working out in real codebases. I'm loving the 40% code reduction compared to manually creating guards for each instance. Imo it validates the usefulness of the API.

@GrigorenkoPV

GrigorenkoPV commented Aug 25, 2026

Copy link
Copy Markdown
Contributor Author

I'm loving the 40% code reduction compared to manually creating guards for each instance.

Not sure how accurate that metric is, but yeah, code reduction is definitely real.

It is especially nice to not have to write all the types out again for the struct fields. And the generics for both the struct and the Drop impl (since those cannot be reused from the outer scope; in one place the old approach even required a PhantomData just to capture one of the generic parameters, which, with this change, is not needed anymore).

Imo it validates the usefulness of the API.

Absolutely. I remember touching std (something to do with Drain or CloneUninit) at some point a few years ago and wondering why there was no "canonical" way to to scope guards.

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

Labels

O-unix Operating system: Unix-like 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.

5 participants