@@ -776,6 +776,79 @@ impl<T: ?Sized> Arc<T> {
776776 this. inner ( ) . strong . load ( SeqCst )
777777 }
778778
779+ /// Increments the strong reference count on the `Arc<T>` associated with the
780+ /// provided pointer by one.
781+ ///
782+ /// # Safety
783+ ///
784+ /// The pointer must have been obtained through `Arc::into_raw`, and the
785+ /// associated `Arc` instance must be valid (i.e. the strong count must be at
786+ /// least 1) for the duration of this method.
787+ ///
788+ /// # Examples
789+ ///
790+ /// ```
791+ /// #![feature(arc_mutate_strong_count)]
792+ ///
793+ /// use std::sync::Arc;
794+ ///
795+ /// let five = Arc::new(5);
796+ ///
797+ /// unsafe {
798+ /// let ptr = Arc::into_raw(five);
799+ /// Arc::incr_strong_count(ptr);
800+ ///
801+ /// // This assertion is deterministic because we haven't shared
802+ /// // the `Arc` between threads.
803+ /// let five = Arc::from_raw(ptr);
804+ /// assert_eq!(2, Arc::strong_count(&five));
805+ /// }
806+ /// ```
807+ #[ inline]
808+ #[ unstable( feature = "arc_mutate_strong_count" , issue = "71983" ) ]
809+ pub unsafe fn incr_strong_count ( ptr : * const T ) {
810+ // Retain Arc, but don't touch refcount by wrapping in ManuallyDrop
811+ let arc = mem:: ManuallyDrop :: new ( Arc :: < T > :: from_raw ( ptr) ) ;
812+ // Now increase refcount, but don't drop new refcount either
813+ let _arc_clone: mem:: ManuallyDrop < _ > = arc. clone ( ) ;
814+ }
815+
816+ /// Decrements the strong reference count on the `Arc<T>` associated with the
817+ /// provided pointer by one.
818+ ///
819+ /// # Safety
820+ ///
821+ /// The pointer must have been obtained through `Arc::into_raw`, and the
822+ /// associated `Arc` instance must be valid (i.e. the strong count must be at
823+ /// least 1) when invoking this method. This method can be used to release the final
824+ /// `Arc` and backing storage, but **should not** be called after the final `Arc` has been
825+ /// released.
826+ ///
827+ /// # Examples
828+ ///
829+ /// ```
830+ /// #![feature(arc_mutate_strong_count)]
831+ ///
832+ /// use std::sync::Arc;
833+ ///
834+ /// let five = Arc::new(5);
835+ ///
836+ /// unsafe {
837+ /// let ptr = Arc::into_raw(five);
838+ /// Arc::decr_strong_count(ptr);
839+ ///
840+ /// // This assertion is deterministic because we haven't shared
841+ /// // the `Arc` between threads.
842+ /// let five = Arc::from_raw(ptr);
843+ /// assert_eq!(0, Arc::strong_count(&five));
844+ /// }
845+ /// ```
846+ #[ inline]
847+ #[ unstable( feature = "arc_mutate_strong_count" , issue = "71983" ) ]
848+ pub unsafe fn decr_strong_count ( ptr : * const T ) {
849+ mem:: drop ( Arc :: from_raw ( ptr) ) ;
850+ }
851+
779852 #[ inline]
780853 fn inner ( & self ) -> & ArcInner < T > {
781854 // This unsafety is ok because while this arc is alive we're guaranteed
0 commit comments