Skip to content

Commit ac37c32

Browse files
Rollup merge of #79285 - yoshuawuyts:stabilize-arc_mutate_strong_count, r=m-ou-se
Stabilize Arc::{increment,decrement}_strong_count 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 * [Initial implementation](#70733) * [Motivation from #68700](#68700 (comment)) * [Real world example in an executor](https://docs.rs/extreme/666.666.666666/src/extreme/lib.rs.html#13)
2 parents 1bf1305 + fe4ac95 commit ac37c32

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

library/alloc/src/sync.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -962,15 +962,13 @@ impl<T: ?Sized> Arc<T> {
962962
/// # Examples
963963
///
964964
/// ```
965-
/// #![feature(arc_mutate_strong_count)]
966-
///
967965
/// use std::sync::Arc;
968966
///
969967
/// let five = Arc::new(5);
970968
///
971969
/// unsafe {
972970
/// let ptr = Arc::into_raw(five);
973-
/// Arc::incr_strong_count(ptr);
971+
/// Arc::increment_strong_count(ptr);
974972
///
975973
/// // This assertion is deterministic because we haven't shared
976974
/// // the `Arc` between threads.
@@ -979,8 +977,8 @@ impl<T: ?Sized> Arc<T> {
979977
/// }
980978
/// ```
981979
#[inline]
982-
#[unstable(feature = "arc_mutate_strong_count", issue = "71983")]
983-
pub unsafe fn incr_strong_count(ptr: *const T) {
980+
#[stable(feature = "arc_mutate_strong_count", since = "1.51.0")]
981+
pub unsafe fn increment_strong_count(ptr: *const T) {
984982
// Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
985983
let arc = unsafe { mem::ManuallyDrop::new(Arc::<T>::from_raw(ptr)) };
986984
// Now increase refcount, but don't drop new refcount either
@@ -1001,27 +999,25 @@ impl<T: ?Sized> Arc<T> {
1001999
/// # Examples
10021000
///
10031001
/// ```
1004-
/// #![feature(arc_mutate_strong_count)]
1005-
///
10061002
/// use std::sync::Arc;
10071003
///
10081004
/// let five = Arc::new(5);
10091005
///
10101006
/// unsafe {
10111007
/// let ptr = Arc::into_raw(five);
1012-
/// Arc::incr_strong_count(ptr);
1008+
/// Arc::increment_strong_count(ptr);
10131009
///
10141010
/// // Those assertions are deterministic because we haven't shared
10151011
/// // the `Arc` between threads.
10161012
/// let five = Arc::from_raw(ptr);
10171013
/// assert_eq!(2, Arc::strong_count(&five));
1018-
/// Arc::decr_strong_count(ptr);
1014+
/// Arc::decrement_strong_count(ptr);
10191015
/// assert_eq!(1, Arc::strong_count(&five));
10201016
/// }
10211017
/// ```
10221018
#[inline]
1023-
#[unstable(feature = "arc_mutate_strong_count", issue = "71983")]
1024-
pub unsafe fn decr_strong_count(ptr: *const T) {
1019+
#[stable(feature = "arc_mutate_strong_count", since = "1.51.0")]
1020+
pub unsafe fn decrement_strong_count(ptr: *const T) {
10251021
unsafe { mem::drop(Arc::from_raw(ptr)) };
10261022
}
10271023

library/alloc/src/task.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<W: Wake + Send + Sync + 'static> From<Arc<W>> for RawWaker {
6262
fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker {
6363
// Increment the reference count of the arc to clone it.
6464
unsafe fn clone_waker<W: Wake + Send + Sync + 'static>(waker: *const ()) -> RawWaker {
65-
unsafe { Arc::incr_strong_count(waker as *const W) };
65+
unsafe { Arc::increment_strong_count(waker as *const W) };
6666
RawWaker::new(
6767
waker as *const (),
6868
&RawWakerVTable::new(clone_waker::<W>, wake::<W>, wake_by_ref::<W>, drop_waker::<W>),
@@ -83,7 +83,7 @@ fn raw_waker<W: Wake + Send + Sync + 'static>(waker: Arc<W>) -> RawWaker {
8383

8484
// Decrement the reference count of the Arc on drop
8585
unsafe fn drop_waker<W: Wake + Send + Sync + 'static>(waker: *const ()) {
86-
unsafe { Arc::decr_strong_count(waker as *const W) };
86+
unsafe { Arc::decrement_strong_count(waker as *const W) };
8787
}
8888

8989
RawWaker::new(

0 commit comments

Comments
 (0)