diff --git a/src/arc.rs b/src/arc.rs index e0f0960..d95b247 100644 --- a/src/arc.rs +++ b/src/arc.rs @@ -108,27 +108,6 @@ impl Arc { f(&transient) } - /// Converts an `Arc` into a `OffsetArc`. This consumes the `Arc`, so the refcount - /// is not modified. - #[inline] - pub fn into_raw_offset(a: Self) -> OffsetArc { - unsafe { - OffsetArc { - ptr: ptr::NonNull::new_unchecked(Arc::into_raw(a) as *mut T), - phantom: PhantomData, - } - } - } - - /// Converts a `OffsetArc` into an `Arc`. This consumes the `OffsetArc`, so the refcount - /// is not modified. - #[inline] - pub fn from_raw_offset(a: OffsetArc) -> Self { - let a = ManuallyDrop::new(a); - let ptr = a.ptr.as_ptr(); - unsafe { Arc::from_raw(ptr) } - } - /// Returns the inner value, if the [`Arc`] has exactly one strong reference. /// /// Otherwise, an [`Err`] is returned with the same [`Arc`] that was @@ -266,6 +245,27 @@ impl Arc { Arc::from_raw_inner(arc_inner_ptr as *mut ArcInner) } + /// Converts a `OffsetArc` into an `Arc`. This consumes the `OffsetArc`, so the refcount + /// is not modified. + #[inline] + pub fn from_raw_offset(a: OffsetArc) -> Self { + let a = ManuallyDrop::new(a); + let ptr = a.ptr.as_ptr(); + unsafe { Arc::from_raw(ptr) } + } + + /// Converts an `Arc` into a `OffsetArc`. This consumes the `Arc`, so the refcount + /// is not modified. + #[inline] + pub fn into_raw_offset(a: Self) -> OffsetArc { + unsafe { + OffsetArc { + ptr: ptr::NonNull::new_unchecked(Arc::into_raw(a) as *mut T), + phantom: PhantomData, + } + } + } + /// Returns the raw pointer. /// /// Same as into_raw except `self` isn't consumed. diff --git a/src/offset_arc.rs b/src/offset_arc.rs index d18715d..5164d5b 100644 --- a/src/offset_arc.rs +++ b/src/offset_arc.rs @@ -29,34 +29,34 @@ use super::{Arc, ArcBorrow}; /// This is very useful if you have an Arc-containing struct shared between Rust and C++, /// and wish for C++ to be able to read the data behind the `Arc` without incurring /// an FFI call overhead. -#[derive(Eq)] #[repr(transparent)] -pub struct OffsetArc { +pub struct OffsetArc { pub(crate) ptr: ptr::NonNull, pub(crate) phantom: PhantomData, } -unsafe impl Send for OffsetArc {} -unsafe impl Sync for OffsetArc {} +unsafe impl Send for OffsetArc {} +unsafe impl Sync for OffsetArc {} -impl UnwindSafe for OffsetArc {} +impl UnwindSafe for OffsetArc {} -impl Deref for OffsetArc { +impl Deref for OffsetArc { type Target = T; + #[inline] fn deref(&self) -> &Self::Target { unsafe { &*self.ptr.as_ptr() } } } -impl Clone for OffsetArc { +impl Clone for OffsetArc { #[inline] fn clone(&self) -> Self { Arc::into_raw_offset(self.clone_arc()) } } -impl Drop for OffsetArc { +impl Drop for OffsetArc { fn drop(&mut self) { let _ = Arc::from_raw_offset(OffsetArc { ptr: self.ptr, @@ -65,13 +65,13 @@ impl Drop for OffsetArc { } } -impl fmt::Debug for OffsetArc { +impl fmt::Debug for OffsetArc { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) } } -impl PartialEq for OffsetArc { +impl PartialEq for OffsetArc { fn eq(&self, other: &OffsetArc) -> bool { *(*self) == *(*other) } @@ -82,22 +82,9 @@ impl PartialEq for OffsetArc { } } -impl OffsetArc { - /// Temporarily converts |self| into a bonafide Arc and exposes it to the - /// provided callback. The refcount is not modified. - #[inline] - pub fn with_arc(&self, f: F) -> U - where - F: FnOnce(&Arc) -> U, - { - // Synthesize transient Arc, which never touches the refcount of the ArcInner. - let transient = unsafe { ManuallyDrop::new(Arc::from_raw(self.ptr.as_ptr())) }; - - // Expose the transient Arc to the callback, which may clone it if it wants - // and forward the result to the user - f(&transient) - } +impl Eq for OffsetArc {} +impl OffsetArc { /// If uniquely owned, provide a mutable reference /// Else create a copy, and mutate that /// @@ -125,6 +112,23 @@ impl OffsetArc { &mut *ret } } +} + +impl OffsetArc { + /// Temporarily converts |self| into a bonafide Arc and exposes it to the + /// provided callback. The refcount is not modified. + #[inline] + pub fn with_arc(&self, f: F) -> U + where + F: FnOnce(&Arc) -> U, + { + // Synthesize transient Arc, which never touches the refcount of the ArcInner. + let transient = unsafe { ManuallyDrop::new(Arc::from_raw(self.ptr.as_ptr())) }; + + // Expose the transient Arc to the callback, which may clone it if it wants + // and forward the result to the user + f(&transient) + } /// Clone it as an `Arc` #[inline]