Skip to content

Add Arc/Rc::strong_count_from_raw - #159098

Open
valentynkit wants to merge 2 commits into
rust-lang:mainfrom
valentynkit:arc-rc-strong-count-from-raw
Open

Add Arc/Rc::strong_count_from_raw#159098
valentynkit wants to merge 2 commits into
rust-lang:mainfrom
valentynkit:arc-rc-strong-count-from-raw

Conversation

@valentynkit

Copy link
Copy Markdown
Contributor

Tracking issue: #157021

Accepted ACP: rust-lang/libs-team#792

Adds strong_count_from_raw to Arc<T, A> and Rc<T, A>. It's the read-only counterpart to increment_strong_count and decrement_strong_count: you can read the strong count straight from a raw pointer that came out of into_raw, instead of rebuilding the smart pointer (and then having to re-leak or drop it) just to peek at the count.

Only the strong count is here. Weak count came up in the ACP thread, and libs-api decided to hold off on it until there's a concrete use case, so it's left out.

There's one spot where I went a different way than the ACP sketch, and I'd like your call on it. The ACP puts the method on the impl<T: ?Sized, A: Allocator> block, but nothing in the signature mentions A. A raw pointer from into_raw has already thrown the allocator type away, so a plain Arc::strong_count_from_raw(ptr) can't infer A, and every caller would be stuck writing a turbofish. I moved it to the A = Global block instead, right next to increment_strong_count and decrement_strong_count, and gave it the same "allocated by the global allocator" safety clause they use. That keeps the calls clean and matches how the other raw-pointer methods are already grouped. If you'd rather it stayed on the generic block, or want an _in variant alongside it, just say so and I'll redo it.

On the body: I read the strong field directly, walking back from the data pointer the way from_raw_in does, rather than rebuilding a throwaway Arc/Rc and calling strong_count. That way the access only touches the counter, not the whole allocation. If you'd prefer the from_raw + strong_count version for consistency with the methods around it, that's an easy swap.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 10, 2026
@valentynkit
valentynkit marked this pull request as ready for review July 11, 2026 07:17
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 11, 2026
@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Jul 11, 2026
@rustbot

rustbot commented Jul 11, 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 Darksonn, JohnTitor, Mark-Simulacrum, clarfonthey, jhpratt

@Mark-Simulacrum

Copy link
Copy Markdown
Member

gave it the same "allocated by the global allocator" safety clause they use

Can you say more about this? Why do you think that requirement is necessary on these functions?

In general, I think I'm OK landing this without the need for users to specify an allocator type; as far as I can tell that has no bearing on the ability to access the strong count.

The clause came from increment_strong_count and decrement_strong_count,
where it holds because those rebuild an Arc<T, Global> through
from_raw_in and the decrement path frees through the global allocator.
Reading the strong count never builds or drops one, and the allocator
lives on the Arc rather than in the allocation, so it does not carry
over.
@valentynkit

Copy link
Copy Markdown
Contributor Author

Can you say more about this? Why do you think that requirement is necessary on these functions?

It isn't. I copied it off increment_strong_count, which needs it because it rebuilds the Arc. This only reads the counter. Dropped it in 5403179, docs only.

Comment thread library/alloc/src/rc.rs
///
/// This method does not consume or drop the `Rc` behind this pointer. To avoid a memory
/// leak, the pointer must be converted back to an `Rc` using [`Rc::from_raw`] or the
/// allocation must be released using [`Rc::decrement_strong_count`].

@Mark-Simulacrum Mark-Simulacrum Aug 3, 2026

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.

This comment about leaks seems copy/pasted and not relevant to the function at hand.

View changes since the review

Comment thread library/alloc/src/rc.rs
/// # Safety
///
/// The pointer must have been obtained through `Rc::into_raw` and must satisfy the
/// same layout requirements specified in [`Rc::from_raw_in`][from_raw_in].

@Mark-Simulacrum Mark-Simulacrum Aug 3, 2026

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.

It doesn't really make sense to say it has to go through into_raw (it's fine to use this with non-Global alloc'd Rc) and from_raw_in is also confusing because we specifically don't have an accurate A parameter here.

View changes since the review

@rustbot rustbot 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 Aug 3, 2026
@rustbot

rustbot commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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