@@ -191,8 +191,7 @@ pub struct Weak<T: ?Sized> {
191191 // but it is not necessarily a valid pointer.
192192 // `Weak::new` sets this to `usize::MAX` so that it doesn’t need
193193 // to allocate space on the heap. That's not a value a real pointer
194- // will ever have because RcBox has alignment at least 2.
195- // This is only possible when `T: Sized`; unsized `T` never dangle.
194+ // will ever have because ArcInner has alignment at least 2.
196195 ptr : NonNull < ArcInner < T > > ,
197196}
198197
@@ -698,7 +697,7 @@ impl<T> Arc<mem::MaybeUninit<T>> {
698697 pub unsafe fn assume_init ( self ) -> Arc < T > {
699698 let ptr = Arc :: into_inner_non_null ( self ) ;
700699 // SAFETY: MaybeUninit<T> has the same layout as T, and
701- // the caller must ensure data is initialized.
700+ // the caller must guarantee that the data is initialized.
702701 unsafe { Arc :: from_inner ( ptr. cast :: < ArcInner < T > > ( ) ) }
703702 }
704703}
@@ -739,7 +738,7 @@ impl<T> Arc<[mem::MaybeUninit<T>]> {
739738 pub unsafe fn assume_init ( self ) -> Arc < [ T ] > {
740739 let ptr = Arc :: into_inner_non_null ( self ) ;
741740 // SAFETY: [MaybeUninit<T>] has the same layout as [T], and
742- // the caller must ensure data is initialized.
741+ // the caller must guarantee that the data is initialized.
743742 unsafe { Arc :: from_ptr ( ptr. as_ptr ( ) as * mut ArcInner < [ T ] > ) }
744743 }
745744}
@@ -764,6 +763,8 @@ impl<T: ?Sized> Arc<T> {
764763 /// different types. See [`mem::transmute`] for more information
765764 /// on what restrictions apply in this case.
766765 ///
766+ /// The raw pointer must point to a block of memory allocated by the global allocator.
767+ ///
767768 /// The user of `from_raw` has to make sure a specific value of `T` is only
768769 /// dropped once.
769770 ///
@@ -822,9 +823,11 @@ impl<T: ?Sized> Arc<T> {
822823 ///
823824 /// # Safety
824825 ///
825- /// The pointer must have been obtained through `Arc::into_raw`, and the
826- /// associated `Arc` instance must be valid (i.e. the strong count must be at
827- /// least 1) for the duration of this method.
826+ /// The pointer must have been obtained through `Arc::into_raw` and must satisfy the
827+ /// same layout requirements specified in [`Arc::from_raw`].
828+ /// The associated `Arc` instance must be valid (i.e. the strong count must be at
829+ /// least 1) for the duration of this method, and `ptr` must point to a block of memory
830+ /// allocated by the global allocator.
828831 ///
829832 /// # Examples
830833 ///
@@ -858,9 +861,11 @@ impl<T: ?Sized> Arc<T> {
858861 ///
859862 /// # Safety
860863 ///
861- /// The pointer must have been obtained through `Arc::into_raw`, and the
862- /// associated `Arc` instance must be valid (i.e. the strong count must be at
863- /// least 1) when invoking this method. This method can be used to release the final
864+ /// The pointer must have been obtained through `Arc::into_raw` and must satisfy the
865+ /// same layout requirements specified in [`Arc::from_raw`].
866+ /// The associated `Arc` instance must be valid (i.e. the strong count must be at
867+ /// least 1) when invoking this method, and `ptr` must point to a block of memory
868+ /// allocated by the global allocator. This method can be used to release the final
864869 /// `Arc` and backing storage, but **should not** be called after the final `Arc` has been
865870 /// released.
866871 ///
@@ -933,6 +938,9 @@ impl<T: ?Sized> Arc<T> {
933938 pub fn as_ptr ( this : & Self ) -> * const T {
934939 let ptr: * mut ArcInner < T > = this. ptr . as_ptr ( ) ;
935940
941+ // SAFETY: This cannot go through Deref::deref or ArcInnerPtr::inner because
942+ // this is required to retain raw/mut provenance such that e.g. `get_mut` can
943+ // write through the pointer after the Arc is recovered through `from_raw`.
936944 unsafe { data_ptr :: < T > ( ptr, & * * this) }
937945 }
938946
@@ -1695,7 +1703,7 @@ struct WeakInner<'a> {
16951703 strong : & ' a atomic:: AtomicUsize ,
16961704}
16971705
1698- // TODO: See Weak::from_raw
1706+ // TODO: See todo comment in Weak::from_raw
16991707impl < T /*: ?Sized */ > Weak < T > {
17001708 /// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>`.
17011709 ///
@@ -1708,7 +1716,7 @@ impl<T /*: ?Sized */> Weak<T> {
17081716 /// # Safety
17091717 ///
17101718 /// The pointer must have originated from the [`into_raw`] and must still own its potential
1711- /// weak reference.
1719+ /// weak reference, and must point to a block of memory allocated by global allocator .
17121720 ///
17131721 /// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
17141722 /// takes ownership of one weak reference currently represented as a raw pointer (the weak
@@ -1753,10 +1761,9 @@ impl<T /*: ?Sized */> Weak<T> {
17531761 // only support sized types that can avoid references to data
17541762 // unless align_of_val_raw is stabilized.
17551763 // // SAFETY: data_offset is safe to call, as ptr references a real (potentially dropped) T.
1756- // let offset = unsafe { data_offset::<T> (ptr) };
1764+ // let offset = unsafe { data_offset(ptr) };
17571765 let offset = data_offset_align ( mem:: align_of :: < T > ( ) ) ;
1758-
1759- // Thus, we reverse the offset to get the whole RcBox.
1766+ // Thus, we reverse the offset to get the whole ArcInner.
17601767 // SAFETY: the pointer originated from a Weak, so this offset is safe.
17611768 unsafe { strict:: byte_sub ( ptr as * mut T , offset) as * mut ArcInner < T > }
17621769 } ;
@@ -1766,7 +1773,7 @@ impl<T /*: ?Sized */> Weak<T> {
17661773 }
17671774}
17681775
1769- // TODO: See Weak::from_raw
1776+ // TODO: See todo comment in Weak::from_raw
17701777impl < T /*: ?Sized */ > Weak < T > {
17711778 /// Returns a raw pointer to the object `T` pointed to by this `Weak<T>`.
17721779 ///
@@ -1803,7 +1810,7 @@ impl<T /*: ?Sized */> Weak<T> {
18031810 // a valid payload address, as the payload is at least as aligned as ArcInner (usize).
18041811 ptr as * const T
18051812 } else {
1806- // TODO: See Weak::from_raw
1813+ // TODO: See todo comment in Weak::from_raw
18071814 // // SAFETY: if is_dangling returns false, then the pointer is dereferenceable.
18081815 // // The payload may be dropped at this point, and we have to maintain provenance,
18091816 // // so use raw pointer manipulation.
@@ -1949,7 +1956,7 @@ impl<T: ?Sized> Weak<T> {
19491956 if is_dangling ( ptr) {
19501957 None
19511958 } else {
1952- // SAFETY: non-dangling Weak is a valid pointer.
1959+ // SAFETY: non-dangling Weak has a valid pointer.
19531960 // We are careful to *not* create a reference covering the "data" field, as
19541961 // the field may be mutated concurrently (for example, if the last `Arc`
19551962 // is dropped, the data field will be dropped in-place).
@@ -2661,12 +2668,12 @@ impl<T> FromIterator<T> for Arc<[T]> {
26612668 /// this behaves as if we wrote:
26622669 ///
26632670 /// ```
2664- /// # #![cfg_attr(rustfmt, rustfmt::skip)] // rustfmt bug that inserts new line before "# ..." when the previous line ends with a comment.
26652671 /// use portable_atomic_util::Arc;
26662672 /// let evens: Arc<[u8]> = (0..10).filter(|&x| x % 2 == 0)
26672673 /// .collect::<Vec<_>>() // The first set of allocations happens here.
26682674 /// .into(); // A second allocation for `Arc<[T]>` happens here.
2669- /// # assert_eq!(&*evens, &[0, 2, 4, 6, 8]);
2675+ ///
2676+ /// assert_eq!(&*evens, &[0, 2, 4, 6, 8]);
26702677 /// ```
26712678 ///
26722679 /// This will allocate as many times as needed for constructing the `Vec<T>`
@@ -2678,10 +2685,10 @@ impl<T> FromIterator<T> for Arc<[T]> {
26782685 /// a single allocation will be made for the `Arc<[T]>`. For example:
26792686 ///
26802687 /// ```
2681- /// # #![cfg_attr(rustfmt, rustfmt::skip)] // rustfmt bug that inserts new line before "# ..." when the previous line ends with a comment.
26822688 /// use portable_atomic_util::Arc;
26832689 /// let evens: Arc<[u8]> = (0..10).collect(); // Just a single allocation happens here.
2684- /// # assert_eq!(&*evens, &*(0..10).collect::<Vec<_>>());
2690+ ///
2691+ /// assert_eq!(&*evens, &*(0..10).collect::<Vec<_>>());
26852692 /// ```
26862693 fn from_iter < I : IntoIterator < Item = T > > ( iter : I ) -> Self {
26872694 iter. into_iter ( ) . collect :: < Vec < T > > ( ) . into ( )
@@ -2719,7 +2726,7 @@ unsafe fn data_ptr<T: ?Sized>(arc: *mut ArcInner<T>, data: &T) -> *mut T {
27192726/// Gets the offset within an `ArcInner` for the payload behind a pointer.
27202727fn data_offset < T : ?Sized > ( ptr : & T ) -> usize {
27212728 // Align the unsized value to the end of the ArcInner.
2722- // Because RcBox is repr(C), it will always be the last field in memory.
2729+ // Because ArcInner is repr(C), it will always be the last field in memory.
27232730 data_offset_align ( mem:: align_of_val :: < T > ( ptr) )
27242731}
27252732
0 commit comments