@@ -1644,8 +1644,8 @@ impl<T> Weak<T> {
16441644
16451645 /// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
16461646 ///
1647- /// The pointer is valid only if there are some strong references. The pointer may be dangling
1648- /// or even [`null`] otherwise.
1647+ /// The pointer is valid only if there are some strong references. The pointer may be dangling,
1648+ /// unaligned or even [`null`] otherwise.
16491649 ///
16501650 /// # Examples
16511651 ///
@@ -1658,31 +1658,22 @@ impl<T> Weak<T> {
16581658 /// let strong = Rc::new("hello".to_owned());
16591659 /// let weak = Rc::downgrade(&strong);
16601660 /// // Both point to the same object
1661- /// assert!(ptr::eq(&*strong, weak.as_raw ()));
1661+ /// assert!(ptr::eq(&*strong, weak.as_ptr ()));
16621662 /// // The strong here keeps it alive, so we can still access the object.
1663- /// assert_eq!("hello", unsafe { &*weak.as_raw () });
1663+ /// assert_eq!("hello", unsafe { &*weak.as_ptr () });
16641664 ///
16651665 /// drop(strong);
1666- /// // But not any more. We can do weak.as_raw (), but accessing the pointer would lead to
1666+ /// // But not any more. We can do weak.as_ptr (), but accessing the pointer would lead to
16671667 /// // undefined behaviour.
1668- /// // assert_eq!("hello", unsafe { &*weak.as_raw () });
1668+ /// // assert_eq!("hello", unsafe { &*weak.as_ptr () });
16691669 /// ```
16701670 ///
16711671 /// [`null`]: ../../std/ptr/fn.null.html
16721672 #[ unstable( feature = "weak_into_raw" , issue = "60728" ) ]
1673- pub fn as_raw ( & self ) -> * const T {
1674- match self . inner ( ) {
1675- None => ptr:: null ( ) ,
1676- Some ( inner) => {
1677- let offset = data_offset_sized :: < T > ( ) ;
1678- let ptr = inner as * const RcBox < T > ;
1679- // Note: while the pointer we create may already point to dropped value, the
1680- // allocation still lives (it must hold the weak point as long as we are alive).
1681- // Therefore, the offset is OK to do, it won't get out of the allocation.
1682- let ptr = unsafe { ( ptr as * const u8 ) . offset ( offset) } ;
1683- ptr as * const T
1684- }
1685- }
1673+ pub fn as_ptr ( & self ) -> * const T {
1674+ let offset = data_offset_sized :: < T > ( ) ;
1675+ let ptr = self . ptr . cast :: < u8 > ( ) . as_ptr ( ) . wrapping_offset ( offset) ;
1676+ ptr as * const T
16861677 }
16871678
16881679 /// Consumes the `Weak<T>` and turns it into a raw pointer.
@@ -1691,7 +1682,7 @@ impl<T> Weak<T> {
16911682 /// can be turned back into the `Weak<T>` with [`from_raw`].
16921683 ///
16931684 /// The same restrictions of accessing the target of the pointer as with
1694- /// [`as_raw `] apply.
1685+ /// [`as_ptr `] apply.
16951686 ///
16961687 /// # Examples
16971688 ///
@@ -1712,10 +1703,10 @@ impl<T> Weak<T> {
17121703 /// ```
17131704 ///
17141705 /// [`from_raw`]: struct.Weak.html#method.from_raw
1715- /// [`as_raw `]: struct.Weak.html#method.as_raw
1706+ /// [`as_ptr `]: struct.Weak.html#method.as_ptr
17161707 #[ unstable( feature = "weak_into_raw" , issue = "60728" ) ]
17171708 pub fn into_raw ( self ) -> * const T {
1718- let result = self . as_raw ( ) ;
1709+ let result = self . as_ptr ( ) ;
17191710 mem:: forget ( self ) ;
17201711 result
17211712 }
@@ -1730,9 +1721,8 @@ impl<T> Weak<T> {
17301721 ///
17311722 /// # Safety
17321723 ///
1733- /// The pointer must have originated from the [`into_raw`] (or [`as_raw`], provided there was
1734- /// a corresponding [`forget`] on the `Weak<T>`) and must still own its potential weak reference
1735- /// count.
1724+ /// The pointer must have originated from the [`into_raw`] and must still own its potential
1725+ /// weak reference count.
17361726 ///
17371727 /// It is allowed for the strong count to be 0 at the time of calling this, but the weak count
17381728 /// must be non-zero or the pointer must have originated from a dangling `Weak<T>` (one created
@@ -1765,7 +1755,6 @@ impl<T> Weak<T> {
17651755 /// [`upgrade`]: struct.Weak.html#method.upgrade
17661756 /// [`Rc`]: struct.Rc.html
17671757 /// [`Weak`]: struct.Weak.html
1768- /// [`as_raw`]: struct.Weak.html#method.as_raw
17691758 /// [`new`]: struct.Weak.html#method.new
17701759 /// [`forget`]: ../../std/mem/fn.forget.html
17711760 #[ unstable( feature = "weak_into_raw" , issue = "60728" ) ]
0 commit comments