@@ -1644,8 +1644,8 @@ impl<T> Weak<T> {
1644
1644
1645
1645
/// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
1646
1646
///
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.
1649
1649
///
1650
1650
/// # Examples
1651
1651
///
@@ -1658,31 +1658,22 @@ impl<T> Weak<T> {
1658
1658
/// let strong = Rc::new("hello".to_owned());
1659
1659
/// let weak = Rc::downgrade(&strong);
1660
1660
/// // Both point to the same object
1661
- /// assert!(ptr::eq(&*strong, weak.as_raw ()));
1661
+ /// assert!(ptr::eq(&*strong, weak.as_ptr ()));
1662
1662
/// // 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 () });
1664
1664
///
1665
1665
/// 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
1667
1667
/// // undefined behaviour.
1668
- /// // assert_eq!("hello", unsafe { &*weak.as_raw () });
1668
+ /// // assert_eq!("hello", unsafe { &*weak.as_ptr () });
1669
1669
/// ```
1670
1670
///
1671
1671
/// [`null`]: ../../std/ptr/fn.null.html
1672
1672
#[ 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
1686
1677
}
1687
1678
1688
1679
/// Consumes the `Weak<T>` and turns it into a raw pointer.
@@ -1691,7 +1682,7 @@ impl<T> Weak<T> {
1691
1682
/// can be turned back into the `Weak<T>` with [`from_raw`].
1692
1683
///
1693
1684
/// The same restrictions of accessing the target of the pointer as with
1694
- /// [`as_raw `] apply.
1685
+ /// [`as_ptr `] apply.
1695
1686
///
1696
1687
/// # Examples
1697
1688
///
@@ -1712,10 +1703,10 @@ impl<T> Weak<T> {
1712
1703
/// ```
1713
1704
///
1714
1705
/// [`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
1716
1707
#[ unstable( feature = "weak_into_raw" , issue = "60728" ) ]
1717
1708
pub fn into_raw ( self ) -> * const T {
1718
- let result = self . as_raw ( ) ;
1709
+ let result = self . as_ptr ( ) ;
1719
1710
mem:: forget ( self ) ;
1720
1711
result
1721
1712
}
@@ -1730,9 +1721,8 @@ impl<T> Weak<T> {
1730
1721
///
1731
1722
/// # Safety
1732
1723
///
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.
1736
1726
///
1737
1727
/// It is allowed for the strong count to be 0 at the time of calling this, but the weak count
1738
1728
/// 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> {
1765
1755
/// [`upgrade`]: struct.Weak.html#method.upgrade
1766
1756
/// [`Rc`]: struct.Rc.html
1767
1757
/// [`Weak`]: struct.Weak.html
1768
- /// [`as_raw`]: struct.Weak.html#method.as_raw
1769
1758
/// [`new`]: struct.Weak.html#method.new
1770
1759
/// [`forget`]: ../../std/mem/fn.forget.html
1771
1760
#[ unstable( feature = "weak_into_raw" , issue = "60728" ) ]
0 commit comments