From d5745609744ab3c395877921f4c4df9ebbd9393b Mon Sep 17 00:00:00 2001 From: EricLBuehler Date: Sun, 22 Oct 2023 15:16:39 -0400 Subject: [PATCH 1/3] Add safety sections, fix clippy warnings --- src/arc.rs | 16 ++++++++++++---- src/arc_borrow.rs | 3 +++ src/unique_arc.rs | 4 ++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/arc.rs b/src/arc.rs index 34fcb2a..97cca63 100644 --- a/src/arc.rs +++ b/src/arc.rs @@ -77,6 +77,10 @@ impl Arc { /// by the atomic count. /// /// It is recommended to use OffsetArc for this + /// + /// # Safety + /// - The given pointer must be a valid pointer to `T` that came from [`Arc::into_raw`]. + /// - After `from_raw`, the pointer must not be accessed. #[inline] pub unsafe fn from_raw(ptr: *const T) -> Self { // FIXME: when `byte_sub` is stabilized, this can accept T: ?Sized. @@ -151,6 +155,10 @@ impl Arc<[T]> { /// [`Arc::from_raw`] should accept unsized types, but this is not trivial to do correctly /// until the feature [`pointer_bytes_offsets`](https://github.com/rust-lang/rust/issues/96283) /// is stabilized. This is stopgap solution for slices. + /// + /// # Safety + /// - The given pointer must be a valid pointer to `[T]` that came from [`Arc::into_raw`]. + /// - After `from_raw_slice`, the pointer must not be accessed. pub unsafe fn from_raw_slice(ptr: *const [T]) -> Self { let len = (*ptr).len(); // Assuming the offset of `T` in `ArcInner` is the same @@ -471,7 +479,7 @@ impl Arc { pub fn make_mut(this: &mut Self) -> &mut T { if !this.is_unique() { // Another pointer exists; clone - *this = Arc::new(T::clone(&this)); + *this = Arc::new(T::clone(this)); } unsafe { @@ -497,7 +505,7 @@ impl Arc { pub fn make_unique(this: &mut Self) -> &mut UniqueArc { if !this.is_unique() { // Another pointer exists; clone - *this = Arc::new(T::clone(&this)); + *this = Arc::new(T::clone(this)); } unsafe { @@ -712,14 +720,14 @@ impl FromIterator for Arc<[A]> { impl borrow::Borrow for Arc { #[inline] fn borrow(&self) -> &T { - &**self + self } } impl AsRef for Arc { #[inline] fn as_ref(&self) -> &T { - &**self + self } } diff --git a/src/arc_borrow.rs b/src/arc_borrow.rs index d53e1a5..ae2bd1c 100644 --- a/src/arc_borrow.rs +++ b/src/arc_borrow.rs @@ -46,6 +46,9 @@ impl<'a, T> ArcBorrow<'a, T> { /// e.g. if we obtain such a reference over FFI /// TODO: should from_ref be relaxed to unsized types? It can't be /// converted back to an Arc right now for unsized types. + /// + /// # Safety + /// - The reference to `T` must be valid #[inline] pub unsafe fn from_ref(r: &'a T) -> Self { ArcBorrow(r) diff --git a/src/unique_arc.rs b/src/unique_arc.rs index 81d2e08..8ace713 100644 --- a/src/unique_arc.rs +++ b/src/unique_arc.rs @@ -108,7 +108,7 @@ impl UniqueArc { /// /// The given `Arc` must have a reference count of exactly one pub(crate) unsafe fn from_arc_ref(arc: &mut Arc) -> &mut Self { - debug_assert_eq!(Arc::count(&arc), 1); + debug_assert_eq!(Arc::count(arc), 1); // Safety: caller guarantees that `arc` is unique, // `UniqueArc` is `repr(transparent)` @@ -191,7 +191,7 @@ impl Deref for UniqueArc { #[inline] fn deref(&self) -> &T { - &*self.0 + &self.0 } } From e20eca421c9446409e677737e4775dee8da66f48 Mon Sep 17 00:00:00 2001 From: EricLBuehler Date: Mon, 23 Oct 2023 17:36:38 -0400 Subject: [PATCH 2/3] Remove one safety section --- src/arc_borrow.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/arc_borrow.rs b/src/arc_borrow.rs index ae2bd1c..d53e1a5 100644 --- a/src/arc_borrow.rs +++ b/src/arc_borrow.rs @@ -46,9 +46,6 @@ impl<'a, T> ArcBorrow<'a, T> { /// e.g. if we obtain such a reference over FFI /// TODO: should from_ref be relaxed to unsized types? It can't be /// converted back to an Arc right now for unsized types. - /// - /// # Safety - /// - The reference to `T` must be valid #[inline] pub unsafe fn from_ref(r: &'a T) -> Self { ArcBorrow(r) From 4762e2c14de2e49b9f98251011e7594eaded6701 Mon Sep 17 00:00:00 2001 From: EricLBuehler Date: Tue, 24 Oct 2023 05:18:45 -0400 Subject: [PATCH 3/3] Update safety section --- src/arc_borrow.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/arc_borrow.rs b/src/arc_borrow.rs index d53e1a5..4e0e77f 100644 --- a/src/arc_borrow.rs +++ b/src/arc_borrow.rs @@ -46,6 +46,8 @@ impl<'a, T> ArcBorrow<'a, T> { /// e.g. if we obtain such a reference over FFI /// TODO: should from_ref be relaxed to unsized types? It can't be /// converted back to an Arc right now for unsized types. + /// # Safety + /// - The reference to `T` must have come from a Triomphe Arc, UniqueArc, or ArcBorrow. #[inline] pub unsafe fn from_ref(r: &'a T) -> Self { ArcBorrow(r)