Skip to content

Add weak_count and strong_count to Rc and Arc #19193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 23, 2014
Merged

Conversation

scialex
Copy link
Contributor

@scialex scialex commented Nov 21, 2014

These functions allow you to see how many weak and strong references
there are to an Arc, Rc, or an rc::Weak. Due to the design of
Arc it is not possible to get the number of weak references of an
arbitrary arc::Weak. Look in arc.rs for a more in-depth explanation.

On arc::Arc and arc::Weak these operations are wait-free and atomic.

This sort of information is useful for creating dynamically cleared caches for use in OS development, for example holding pages of files in memory until the address space is needed for something else.

@emberian
Copy link
Member

I believe the methods should explicitly be #[experimental] for now. But, this looks good to me. I'll defer final r+ to @alexcrichton or @brson, who understand the concurrency aspects far better than I.

These functions allow you to see how many weak and strong references
there are to an `Arc`, `Rc`, or an `rc::Weak`. Due to the design of
`Arc` it is not possible to get the number of weak references of an
arbitrary `arc::Weak`. Look in `arc.rs` for a more in-depth explanation.

On `arc::Arc` and `arc::Weak` these operations are wait-free and atomic.
@alexcrichton
Copy link
Member

We've been hesitant in the past to add new inherent methods to Rc and Arc (smart pointers in general) due to how they interact with Deref and friends. For example, we have get_mut and is_unique as standalone free functions instead of inherent methods.

Could you move these methods to free functions instead?

@scialex
Copy link
Contributor Author

scialex commented Nov 21, 2014

@alexcrichton I could. The fact that they are for both strong and weak references would open name conflicts (Although I could just remove the Weak versions since they will likely be less useful).

The readability of these as standalone would go way down also (and I honestly doubt many people are using names like this in any case).

@alexcrichton
Copy link
Member

Yeah it's probably fine to remove the weak versions because a failed upgrade means there are 0 strong references and an otherwise successful upgrade allows you to get the active number of strong/weak references.

I do agree that they're not quite as nice to call (as they're not methods and require an import). We haven't closely considered methods such as this in terms of stabilization just yet more than "they should be experimental free functions for now". We may investigate an extension trait, for example, later in the future, but for now free functions follow the existing design of these modules.

Remove both `strong_count` and `weak_count` from `Weak`s and make the
methods bare functions so as not to cause trouble with `deref`.
@scialex
Copy link
Contributor Author

scialex commented Nov 21, 2014

@alexcrichton updated it so that there are only functions for Rc and Arc and the functions are top level.

@tomjakubowski
Copy link
Contributor

cc @steveklabnik, this PR introduces a new method on Rc for #19112 (also, "this value" 😄)

@bors bors merged commit 69861df into rust-lang:master Nov 23, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants