File tree Expand file tree Collapse file tree 2 files changed +50
-2
lines changed Expand file tree Collapse file tree 2 files changed +50
-2
lines changed Original file line number Diff line number Diff line change @@ -569,9 +569,33 @@ impl<T: ?Sized> Rc<T> {
569569 /// ```
570570 #[ stable( feature = "rc_raw" , since = "1.17.0" ) ]
571571 pub fn into_raw ( this : Self ) -> * const T {
572+ let ptr = Self :: as_ptr ( & this) ;
573+ mem:: forget ( this) ;
574+ ptr
575+ }
576+
577+ /// Provides a raw pointer to the data.
578+ ///
579+ /// The counts are not affected in any way and the `Rc` is not consumed. The pointer is valid
580+ /// for as long there are strong counts in the `Rc`.
581+ ///
582+ /// # Examples
583+ ///
584+ /// ```
585+ /// #![feature(weak_into_raw)]
586+ ///
587+ /// use std::rc::Rc;
588+ ///
589+ /// let x = Rc::new("hello".to_owned());
590+ /// let y = Rc::clone(&x);
591+ /// let x_ptr = Rc::as_ptr(&x);
592+ /// assert_eq!(x_ptr, Rc::as_ptr(&y));
593+ /// assert_eq!(unsafe { &*x_ptr }, "hello");
594+ /// ```
595+ #[ unstable( feature = "weak_into_raw" , issue = "60728" ) ]
596+ pub fn as_ptr ( this : & Self ) -> * const T {
572597 let ptr: * mut RcBox < T > = NonNull :: as_ptr ( this. ptr ) ;
573598 let fake_ptr = ptr as * mut T ;
574- mem:: forget ( this) ;
575599
576600 // SAFETY: This cannot go through Deref::deref.
577601 // Instead, we manually offset the pointer rather than manifesting a reference.
Original file line number Diff line number Diff line change @@ -566,9 +566,33 @@ impl<T: ?Sized> Arc<T> {
566566 /// ```
567567 #[ stable( feature = "rc_raw" , since = "1.17.0" ) ]
568568 pub fn into_raw ( this : Self ) -> * const T {
569+ let ptr = Self :: as_ptr ( & this) ;
570+ mem:: forget ( this) ;
571+ ptr
572+ }
573+
574+ /// Provides a raw pointer to the data.
575+ ///
576+ /// The counts are not affected in way and the `Arc` is not consumed. The pointer is valid for
577+ /// as long as there are strong counts in the `Arc`.
578+ ///
579+ /// # Examples
580+ ///
581+ /// ```
582+ /// #![feature(weak_into_raw)]
583+ ///
584+ /// use std::sync::Arc;
585+ ///
586+ /// let x = Arc::new("hello".to_owned());
587+ /// let y = Arc::clone(&x);
588+ /// let x_ptr = Arc::as_ptr(&x);
589+ /// assert_eq!(x_ptr, Arc::as_ptr(&y));
590+ /// assert_eq!(unsafe { &*x_ptr }, "hello");
591+ /// ```
592+ #[ unstable( feature = "weak_into_raw" , issue = "60728" ) ]
593+ pub fn as_ptr ( this : & Self ) -> * const T {
569594 let ptr: * mut ArcInner < T > = NonNull :: as_ptr ( this. ptr ) ;
570595 let fake_ptr = ptr as * mut T ;
571- mem:: forget ( this) ;
572596
573597 // SAFETY: This cannot go through Deref::deref.
574598 // Instead, we manually offset the pointer rather than manifesting a reference.
You can’t perform that action at this time.
0 commit comments