Skip to content

Commit

Permalink
Merge pull request #73 from EricLBuehler/formatting
Browse files Browse the repository at this point in the history
Add safety sections, fix clippy warnings
  • Loading branch information
Manishearth committed Oct 24, 2023
2 parents 3df7b3f + 4762e2c commit f78899c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
16 changes: 12 additions & 4 deletions src/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ impl<T> Arc<T> {
/// 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.
Expand Down Expand Up @@ -151,6 +155,10 @@ impl<T> 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<T>` is the same
Expand Down Expand Up @@ -471,7 +479,7 @@ impl<T: Clone> Arc<T> {
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 {
Expand All @@ -497,7 +505,7 @@ impl<T: Clone> Arc<T> {
pub fn make_unique(this: &mut Self) -> &mut UniqueArc<T> {
if !this.is_unique() {
// Another pointer exists; clone
*this = Arc::new(T::clone(&this));
*this = Arc::new(T::clone(this));
}

unsafe {
Expand Down Expand Up @@ -712,14 +720,14 @@ impl<A> FromIterator<A> for Arc<[A]> {
impl<T: ?Sized> borrow::Borrow<T> for Arc<T> {
#[inline]
fn borrow(&self) -> &T {
&**self
self
}
}

impl<T: ?Sized> AsRef<T> for Arc<T> {
#[inline]
fn as_ref(&self) -> &T {
&**self
self
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/arc_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/unique_arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<T: ?Sized> UniqueArc<T> {
///
/// The given `Arc` must have a reference count of exactly one
pub(crate) unsafe fn from_arc_ref(arc: &mut Arc<T>) -> &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)`
Expand Down Expand Up @@ -191,7 +191,7 @@ impl<T: ?Sized> Deref for UniqueArc<T> {

#[inline]
fn deref(&self) -> &T {
&*self.0
&self.0
}
}

Expand Down

0 comments on commit f78899c

Please sign in to comment.