Skip to content
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

Stabilize Arc::{increment,decrement}_strong_count #79285

Merged
merged 2 commits into from
Jan 31, 2021
Merged

Stabilize Arc::{increment,decrement}_strong_count #79285

merged 2 commits into from
Jan 31, 2021

Conversation

yoshuawuyts
Copy link
Member

@yoshuawuyts yoshuawuyts commented Nov 22, 2020

Tracking issue: #71983

Stabilizes Arc::{incr,decr}_strong_count, enabling unsafely incrementing an decrementing the Arc strong count directly with fewer gotchas. This API was first introduced on nightly six months ago, and has not seen any changes since. The initial PR showed two existing pieces of code that would benefit from this API, and included a change inside the stdlib to use this.

Given the small surface area, predictable use, and no changes since introduction, I'd like to propose we stabilize this.

closes #71983
r? @Mark-Simulacrum

Links

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 22, 2020
@Mark-Simulacrum
Copy link
Member

I am forgetting now -- is there a reason we didn't add this on Rc? I don't quickly see any discussion about that, but it seems like they'd be equally useful there ergonomics wise, though it's somewhat rarer (I suspect) to be stashing away Rc's in atomics or whatever where you're hiding them.

@yoshuawuyts
Copy link
Member Author

yoshuawuyts commented Nov 22, 2020

@Mark-Simulacrum I don't believe we discussed it, but there's no reason not to as far as I recall — I'm planning to add it to Rc and both Weak variants as a follow-up.

If there are no objections to stabilizing the Arc methods though, it'd be nice if we could stabilize that sooner.

@Mark-Simulacrum
Copy link
Member

This stabilizes the following methods on Arc, intended for manipulating the strong reference count without error-prone code required before (e.g., cloning the arc and forgetting it to increment or double-dropping an arc to decrement).

impl<T: ?Sized> Arc<T> {
    pub unsafe fn incr_strong_count(ptr: *const T);
    pub unsafe fn decr_strong_count(ptr: *const T);
}

The only reason these APIs are unsafe is that we take *const T rather than an Arc<T> or &Arc<T>; they are almost always used after Arc::into_raw, so this unsafety is deemed warranted. If we were to take an actual instance of Arc<T> the methods would be less useful as the caller would need to be careful to not re-drop the Arc in the case of &Arc<T> and/or not clone the Arc on passing it in in the owned case. Generally it's expected that this API is easier to use.

r? @dtolnay to kick off libs FCP on this.

@Mark-Simulacrum Mark-Simulacrum added needs-fcp This change is insta-stable, so needs a completed FCP to proceed. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Nov 22, 2020
@nagisa
Copy link
Member

nagisa commented Nov 23, 2020

Should incr and decr be expanded to a proper word before stabilizing?

@yoshuawuyts
Copy link
Member Author

yoshuawuyts commented Dec 4, 2020

@nagisa I find that increment_strong_count and decrement_strong_count feel rather verbose and would prefer we keep the naming as-is. The current names also have a nice benefit that they could also stand for "increase" and "decrease" which may make them easier to memorize.

@dtolnay
Copy link
Member

dtolnay commented Dec 6, 2020

I am on board with these. #70733 has compelling discussion that this functionality is worth exposing in the standard library, and that these signatures involving *const T are the best ones.

@rfcbot fcp merge

@rfcbot
Copy link

rfcbot commented Dec 6, 2020

Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Dec 6, 2020
@BurntSushi
Copy link
Member

RE naming: I do have a mild preference for increment/decrement instead of abbreviating them. While these are useful, they are niche, so I don't really buy the verbosity argument. I don't think it's that important to reduce verbosity for infrequently used routines.

RE docs: Would it be possible to add some clarity to the docs explaining specifically why someone might want to use these routines?

@tmiasko
Copy link
Contributor

tmiasko commented Dec 6, 2020

Personally I found those functions to be rather underwhelming. They cater to relatively niche use-cases, while having a trivial implementation. Furthermore, their own implementation uses a pattern which is much more generally applicable:

  • decrement: Arc::from_raw(ptr);
  • increment: let _ = ManuallyDrop::new(Arc::from_raw(ptr)).clone();

@Mark-Simulacrum
Copy link
Member

I would call neither of those trivial or easy to follow -- if I see those in the wild, I'm definitely going to expect a comment on them explaining them. I also don't think we guarantee that they will work (there's not really a way we could break that even if we wanted to and ignoring stability guarantees); I've always been rather uncertain about the "clone to increment" pattern externally from std, while internally we can of course use it.

@tmiasko
Copy link
Contributor

tmiasko commented Dec 6, 2020

I also don't think we guarantee that they will work (there's not really a way we could break that even if we wanted to and ignoring stability guarantees); I've always been rather uncertain about the "clone to increment" pattern externally from std, while internally we can of course use it.

I don't think there is anything contentious about Arc::from_raw or clone. The only part that could be controversial is using ManuallyDrop instead of Arc::into_raw. EDIT: Which might be a sign that a Arc::clone_raw might be more appropriate than Arc::incr_strong_count.

@m-ou-se
Copy link
Member

m-ou-se commented Dec 7, 2020

@rfcbot concern naming

See #79285 (comment).

I agree not abbreviating the names would be a bit better. Registering it as a concern so we don't forget.

@withoutboats
Copy link
Contributor

I struggled with the pointer aspect of this API a bit. It seemed strange to combine two steps - offseting to get an arc, and then manipulating the strong count - into one function. Especially so because incrementing the strong count is safe. I was worried that these were overly-tailored to implementing a waker.

What won me over to the current API was the drop problem: users who have a raw pointer have to make sure if they get an Arc from it that they know not to drop it. This is really tricky to get right because its all implicit; implementing it properly in std seems like the right choice.

@yoshuawuyts
Copy link
Member Author

It seems everyone on the libs team has ticked their box! -- but the one outstanding concern is naming. So I'd like to ask: does anyone feel strongly against increment_strong_count, decrement_strong_count?

I've already expressed I prefer the short versions, but I don't feel strong enough about them to block this PR on that. So I want to check if anyone objects to the long-form instead. If not, I'll happily switch this PR over to use increment_strong_count, decrement_strong_count so it can be merged. Thanks!

@yoshuawuyts yoshuawuyts changed the title Stabilize Arc::{incr,decr}_strong_count Stabilize Arc::{increment,decrement}_strong_count Dec 18, 2020
@yoshuawuyts
Copy link
Member Author

I've update the PR to use the {increment,decrement}_strong_count method names. I believe once CI passes this should be ready for FCP.

@m-ou-se
Copy link
Member

m-ou-se commented Dec 18, 2020

@rfcbot resolve naming

@rfcbot rfcbot added the final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. label Dec 18, 2020
@rfcbot
Copy link

rfcbot commented Dec 18, 2020

🔔 This is now entering its final comment period, as per the review above. 🔔

@Mark-Simulacrum
Copy link
Member

The RawWaker API would need to forget the resulting Arc rather than making use of it in the clone "method". I think most use cases for this API aren't actually creating Arcs directly. The uses I've had historically have also done similar things to the RawWaker API, for example across the FFI boundary where either side wants to increment the counter on a shared resource, due to cloning, but isn't actually going to pass back the Arc (because nothing is going to interact with the Arc directly). That said I don't disagree that the clone_from_raw API is in some cases a bit cleaner; I think I would rather add both though. These methods are largely aliases for 1-3 lines of code, we can expose more than one incrementing method if we want. But I think there are valid uses for both just incrementing and for incrementing and getting a reference.

@m-ou-se
Copy link
Member

m-ou-se commented Dec 28, 2020

@SimonSapin I think you have to be on rfcbot's list to add concerns.

@rfcbot concern clone_from_raw instead

#79285 (comment)

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Dec 28, 2020
@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 15, 2021
@m-ou-se
Copy link
Member

m-ou-se commented Jan 20, 2021

@SimonSapin Since this is blocked on your concern, can you respond to Mark's comment? Thanks!

@SimonSapin
Copy link
Contributor

The RawWaker API would need to forget the resulting Arc rather than making use of it in the clone "method". I think most use cases for this API aren't actually creating Arcs directly.

Ok, I didn’t realize that.

This concern can be marked as resolved.

@m-ou-se
Copy link
Member

m-ou-se commented Jan 20, 2021

@rfcbot resolve clone_from_raw instead

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Jan 20, 2021
@rfcbot
Copy link

rfcbot commented Jan 20, 2021

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Jan 30, 2021
@rfcbot
Copy link

rfcbot commented Jan 30, 2021

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

The RFC will be merged soon.

@rfcbot rfcbot added the to-announce Announce this issue on triage meeting label Jan 30, 2021
library/alloc/src/sync.rs Outdated Show resolved Hide resolved
library/alloc/src/sync.rs Outdated Show resolved Hide resolved
@m-ou-se
Copy link
Member

m-ou-se commented Jan 30, 2021

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jan 30, 2021

📌 Commit fe4ac95 has been approved by m-ou-se

@bors bors 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 Jan 30, 2021
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 31, 2021
…as-schievink

Rollup of 18 pull requests

Successful merges:

 - rust-lang#78044 (Implement io::Seek for io::Empty)
 - rust-lang#79285 (Stabilize Arc::{increment,decrement}_strong_count)
 - rust-lang#80053 (stabilise `cargo test -- --include-ignored`)
 - rust-lang#80279 (Implement missing `AsMut<str>` for `str`)
 - rust-lang#80470 (Stabilize by-value `[T; N]` iterator `core::array::IntoIter`)
 - rust-lang#80945 (Add Box::downcast() for dyn Any + Send + Sync)
 - rust-lang#81048 (Stabilize `core::slice::fill_with`)
 - rust-lang#81198 (Remove requirement that forces symmetric and transitive PartialEq impls to exist)
 - rust-lang#81422 (Account for existing `_` field pattern when suggesting `..`)
 - rust-lang#81472 (Clone entire `TokenCursor` when collecting tokens)
 - rust-lang#81484 (Optimize decimal formatting of 128-bit integers)
 - rust-lang#81491 (Balance sidebar `Deref` cycle check with main content)
 - rust-lang#81509 (Add a regression test for ICE of bad_placeholder_type)
 - rust-lang#81547 (Edit rustc_typeck top-level docs)
 - rust-lang#81550 (Replace predecessor with range in collections documentation)
 - rust-lang#81558 (Fix ascii art text wrapping in mobile)
 - rust-lang#81562 (Clarify that InPlaceIterable guarantees extend to all advancing iterator methods.)
 - rust-lang#81563 (Improve docblock readability on small screen)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ac37c32 into rust-lang:master Jan 31, 2021
@rustbot rustbot added this to the 1.51.0 milestone Jan 31, 2021
@yoshuawuyts yoshuawuyts deleted the stabilize-arc_mutate_strong_count branch January 31, 2021 12:02
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Feb 11, 2021
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Apr 7, 2021
…ou-se

Add strong_count mutation methods to Rc

The corresponding methods were stabilized on `Arc` in rust-lang#79285 (tracking: rust-lang#71983). This patch implements and stabilizes identical methods on the `Rc` types as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. needs-fcp This change is insta-stable, so needs a completed FCP to proceed. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

Tracking Issue for feature arc_mutate_strong_count