Blame servo/components/servo_arc/lib.rs

Packit f0b94e
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
Packit f0b94e
// file at the top-level directory of this distribution and at
Packit f0b94e
// http://rust-lang.org/COPYRIGHT.
Packit f0b94e
//
Packit f0b94e
// Licensed under the Apache License, Version 2.0 
Packit f0b94e
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
Packit f0b94e
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
Packit f0b94e
// option. This file may not be copied, modified, or distributed
Packit f0b94e
// except according to those terms.
Packit f0b94e
Packit f0b94e
//! Fork of Arc for Servo. This has the following advantages over std::sync::Arc:
Packit f0b94e
//!
Packit f0b94e
//! * We don't waste storage on the weak reference count.
Packit f0b94e
//! * We don't do extra RMU operations to handle the possibility of weak references.
Packit f0b94e
//! * We can experiment with arena allocation (todo).
Packit f0b94e
//! * We can add methods to support our custom use cases [1].
Packit f0b94e
//! * We have support for dynamically-sized types (see from_header_and_iter).
Packit f0b94e
//! * We have support for thin arcs to unsized types (see ThinArc).
Packit f0b94e
//!
Packit f0b94e
//! [1]: https://bugzilla.mozilla.org/show_bug.cgi?id=1360883
Packit f0b94e
Packit f0b94e
// The semantics of Arc are alread documented in the Rust docs, so we don't
Packit f0b94e
// duplicate those here.
Packit f0b94e
#![allow(missing_docs)]
Packit f0b94e
Packit f0b94e
extern crate nodrop;
Packit f0b94e
#[cfg(feature = "servo")] extern crate serde;
Packit f0b94e
extern crate stable_deref_trait;
Packit f0b94e
Packit f0b94e
use nodrop::NoDrop;
Packit f0b94e
#[cfg(feature = "servo")]
Packit f0b94e
use serde::{Deserialize, Serialize};
Packit f0b94e
use stable_deref_trait::{CloneStableDeref, StableDeref};
Packit f0b94e
use std::{isize, usize};
Packit f0b94e
use std::borrow;
Packit f0b94e
use std::cmp::Ordering;
Packit f0b94e
use std::convert::From;
Packit f0b94e
use std::fmt;
Packit f0b94e
use std::hash::{Hash, Hasher};
Packit f0b94e
use std::iter::{ExactSizeIterator, Iterator};
Packit f0b94e
use std::mem;
Packit f0b94e
use std::ops::{Deref, DerefMut};
Packit f0b94e
use std::os::raw::c_void;
Packit f0b94e
use std::process;
Packit f0b94e
use std::ptr;
Packit f0b94e
use std::slice;
Packit f0b94e
use std::sync::atomic;
Packit f0b94e
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release};
Packit f0b94e
Packit f0b94e
// Private macro to get the offset of a struct field in bytes from the address of the struct.
Packit f0b94e
macro_rules! offset_of {
Packit f0b94e
    ($container:path, $field:ident) => {{
Packit f0b94e
        // Make sure the field actually exists. This line ensures that a compile-time error is
Packit f0b94e
        // generated if $field is accessed through a Deref impl.
Packit f0b94e
        let $container { $field: _, .. };
Packit f0b94e
Packit f0b94e
        // Create an (invalid) instance of the container and calculate the offset to its
Packit f0b94e
        // field. Using a null pointer might be UB if `&(*(0 as *const T)).field` is interpreted to
Packit f0b94e
        // be nullptr deref.
Packit f0b94e
        let invalid: $container = ::std::mem::uninitialized();
Packit f0b94e
        let offset = &invalid.$field as *const _ as usize - &invalid as *const _ as usize;
Packit f0b94e
Packit f0b94e
        // Do not run destructors on the made up invalid instance.
Packit f0b94e
        ::std::mem::forget(invalid);
Packit f0b94e
        offset as isize
Packit f0b94e
    }};
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// A soft limit on the amount of references that may be made to an `Arc`.
Packit f0b94e
///
Packit f0b94e
/// Going above this limit will abort your program (although not
Packit f0b94e
/// necessarily) at _exactly_ `MAX_REFCOUNT + 1` references.
Packit f0b94e
const MAX_REFCOUNT: usize = (isize::MAX) as usize;
Packit f0b94e
Packit f0b94e
/// Wrapper type for pointers to get the non-zero optimization. When
Packit f0b94e
/// NonZero/Shared/Unique are stabilized, we should just use Shared
Packit f0b94e
/// here to get the same effect. Gankro is working on this in [1].
Packit f0b94e
///
Packit f0b94e
/// It's unfortunate that this needs to infect all the caller types
Packit f0b94e
/// with 'static. It would be nice to just use a &() and a PhantomData<T>
Packit f0b94e
/// instead, but then the compiler can't determine whether the &() should
Packit f0b94e
/// be thin or fat (which depends on whether or not T is sized). Given
Packit f0b94e
/// that this is all a temporary hack, this restriction is fine for now.
Packit f0b94e
///
Packit f0b94e
/// [1]: https://github.com/rust-lang/rust/issues/27730
Packit f0b94e
// FIXME: remove this and use std::ptr::NonNull when Firefox requires Rust 1.25+
Packit f0b94e
pub struct NonZeroPtrMut<T: ?Sized + 'static>(&'static mut T);
Packit f0b94e
impl<T: ?Sized> NonZeroPtrMut<T> {
Packit f0b94e
    pub fn new(ptr: *mut T) -> Self {
Packit f0b94e
        assert!(!(ptr as *mut u8).is_null());
Packit f0b94e
        NonZeroPtrMut(unsafe { mem::transmute(ptr) })
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    pub fn ptr(&self) -> *mut T {
Packit f0b94e
        self.0 as *const T as *mut T
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized + 'static> Clone for NonZeroPtrMut<T> {
Packit f0b94e
    fn clone(&self) -> Self {
Packit f0b94e
        NonZeroPtrMut::new(self.ptr())
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized + 'static> fmt::Pointer for NonZeroPtrMut<T> {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        fmt::Pointer::fmt(&self.ptr(), f)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized + 'static> fmt::Debug for NonZeroPtrMut<T> {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        <Self as fmt::Pointer>::fmt(self, f)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized + 'static> PartialEq for NonZeroPtrMut<T> {
Packit f0b94e
    fn eq(&self, other: &Self) -> bool {
Packit f0b94e
        self.ptr() == other.ptr()
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized + 'static> Eq for NonZeroPtrMut<T> {}
Packit f0b94e
Packit f0b94e
impl<T: Sized + 'static> Hash for NonZeroPtrMut<T> {
Packit f0b94e
    fn hash<H: Hasher>(&self, state: &mut H) {
Packit f0b94e
        self.ptr().hash(state)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
#[repr(C)]
Packit f0b94e
pub struct Arc<T: ?Sized + 'static> {
Packit f0b94e
    p: NonZeroPtrMut<ArcInner<T>>,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// An Arc that is known to be uniquely owned
Packit f0b94e
///
Packit f0b94e
/// This lets us build arcs that we can mutate before
Packit f0b94e
/// freezing, without needing to change the allocation
Packit f0b94e
pub struct UniqueArc<T: ?Sized + 'static>(Arc<T>);
Packit f0b94e
Packit f0b94e
impl<T> UniqueArc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    /// Construct a new UniqueArc
Packit f0b94e
    pub fn new(data: T) -> Self {
Packit f0b94e
        UniqueArc(Arc::new(data))
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    #[inline]
Packit f0b94e
    /// Convert to a shareable Arc<T> once we're done using it
Packit f0b94e
    pub fn shareable(self) -> Arc<T> {
Packit f0b94e
        self.0
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T> Deref for UniqueArc<T> {
Packit f0b94e
    type Target = T;
Packit f0b94e
    fn deref(&self) -> &T {
Packit f0b94e
        &*self.0
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T> DerefMut for UniqueArc<T> {
Packit f0b94e
    fn deref_mut(&mut self) -> &mut T {
Packit f0b94e
        // We know this to be uniquely owned
Packit f0b94e
        unsafe { &mut (*self.0.ptr()).data }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
unsafe impl<T: ?Sized + Sync + Send> Send for Arc<T> {}
Packit f0b94e
unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
Packit f0b94e
Packit f0b94e
#[repr(C)]
Packit f0b94e
struct ArcInner<T: ?Sized> {
Packit f0b94e
    count: atomic::AtomicUsize,
Packit f0b94e
    data: T,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
unsafe impl<T: ?Sized + Sync + Send> Send for ArcInner<T> {}
Packit f0b94e
unsafe impl<T: ?Sized + Sync + Send> Sync for ArcInner<T> {}
Packit f0b94e
Packit f0b94e
impl<T> Arc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn new(data: T) -> Self {
Packit f0b94e
        let x = Box::new(ArcInner {
Packit f0b94e
            count: atomic::AtomicUsize::new(1),
Packit f0b94e
            data: data,
Packit f0b94e
        });
Packit f0b94e
        Arc { p: NonZeroPtrMut::new(Box::into_raw(x)) }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn into_raw(this: Self) -> *const T {
Packit f0b94e
        let ptr = unsafe { &((*this.ptr()).data) as *const _ };
Packit f0b94e
        mem::forget(this);
Packit f0b94e
        ptr
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    #[inline]
Packit f0b94e
    unsafe fn from_raw(ptr: *const T) -> Self {
Packit f0b94e
        // To find the corresponding pointer to the `ArcInner` we need
Packit f0b94e
        // to subtract the offset of the `data` field from the pointer.
Packit f0b94e
        let ptr = (ptr as *const u8).offset(-offset_of!(ArcInner<T>, data));
Packit f0b94e
        Arc {
Packit f0b94e
            p: NonZeroPtrMut::new(ptr as *mut ArcInner<T>),
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Produce a pointer to the data that can be converted back
Packit f0b94e
    /// to an arc
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn borrow_arc<'a>(&'a self) -> ArcBorrow<'a, T> {
Packit f0b94e
        ArcBorrow(&**self)
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Temporarily converts |self| into a bonafide RawOffsetArc and exposes it to the
Packit f0b94e
    /// provided callback. The refcount is not modified.
Packit f0b94e
    #[inline(always)]
Packit f0b94e
    pub fn with_raw_offset_arc<F, U>(&self, f: F) -> U
Packit f0b94e
        where F: FnOnce(&RawOffsetArc<T>) -> U
Packit f0b94e
    {
Packit f0b94e
        // Synthesize transient Arc, which never touches the refcount of the ArcInner.
Packit f0b94e
        let transient = unsafe { NoDrop::new(Arc::into_raw_offset(ptr::read(self))) };
Packit f0b94e
Packit f0b94e
        // Expose the transient Arc to the callback, which may clone it if it wants.
Packit f0b94e
        let result = f(&transient);
Packit f0b94e
Packit f0b94e
        // Forget the transient Arc to leave the refcount untouched.
Packit f0b94e
        mem::forget(transient);
Packit f0b94e
Packit f0b94e
        // Forward the result.
Packit f0b94e
        result
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Returns the address on the heap of the Arc itself -- not the T within it -- for memory
Packit f0b94e
    /// reporting.
Packit f0b94e
    pub fn heap_ptr(&self) -> *const c_void {
Packit f0b94e
        self.p.ptr() as *const ArcInner<T> as *const c_void
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized> Arc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn inner(&self) -> &ArcInner<T> {
Packit f0b94e
        // This unsafety is ok because while this arc is alive we're guaranteed
Packit f0b94e
        // that the inner pointer is valid. Furthermore, we know that the
Packit f0b94e
        // `ArcInner` structure itself is `Sync` because the inner data is
Packit f0b94e
        // `Sync` as well, so we're ok loaning out an immutable pointer to these
Packit f0b94e
        // contents.
Packit f0b94e
        unsafe { &*self.ptr() }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    // Non-inlined part of `drop`. Just invokes the destructor.
Packit f0b94e
    #[inline(never)]
Packit f0b94e
    unsafe fn drop_slow(&mut self) {
Packit f0b94e
        let _ = Box::from_raw(self.ptr());
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn ptr_eq(this: &Self, other: &Self) -> bool {
Packit f0b94e
        this.ptr() == other.ptr()
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    fn ptr(&self) -> *mut ArcInner<T> {
Packit f0b94e
        self.p.ptr()
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized> Clone for Arc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn clone(&self) -> Self {
Packit f0b94e
        // Using a relaxed ordering is alright here, as knowledge of the
Packit f0b94e
        // original reference prevents other threads from erroneously deleting
Packit f0b94e
        // the object.
Packit f0b94e
        //
Packit f0b94e
        // As explained in the [Boost documentation][1], Increasing the
Packit f0b94e
        // reference counter can always be done with memory_order_relaxed: New
Packit f0b94e
        // references to an object can only be formed from an existing
Packit f0b94e
        // reference, and passing an existing reference from one thread to
Packit f0b94e
        // another must already provide any required synchronization.
Packit f0b94e
        //
Packit f0b94e
        // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html)
Packit f0b94e
        let old_size = self.inner().count.fetch_add(1, Relaxed);
Packit f0b94e
Packit f0b94e
        // However we need to guard against massive refcounts in case someone
Packit f0b94e
        // is `mem::forget`ing Arcs. If we don't do this the count can overflow
Packit f0b94e
        // and users will use-after free. We racily saturate to `isize::MAX` on
Packit f0b94e
        // the assumption that there aren't ~2 billion threads incrementing
Packit f0b94e
        // the reference count at once. This branch will never be taken in
Packit f0b94e
        // any realistic program.
Packit f0b94e
        //
Packit f0b94e
        // We abort because such a program is incredibly degenerate, and we
Packit f0b94e
        // don't care to support it.
Packit f0b94e
        if old_size > MAX_REFCOUNT {
Packit f0b94e
            process::abort();
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        Arc { p: NonZeroPtrMut::new(self.ptr()) }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized> Deref for Arc<T> {
Packit f0b94e
    type Target = T;
Packit f0b94e
Packit f0b94e
    #[inline]
Packit f0b94e
    fn deref(&self) -> &T {
Packit f0b94e
        &self.inner().data
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: Clone> Arc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn make_mut(this: &mut Self) -> &mut T {
Packit f0b94e
        if !this.is_unique() {
Packit f0b94e
            // Another pointer exists; clone
Packit f0b94e
            *this = Arc::new((**this).clone());
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        unsafe {
Packit f0b94e
            // This unsafety is ok because we're guaranteed that the pointer
Packit f0b94e
            // returned is the *only* pointer that will ever be returned to T. Our
Packit f0b94e
            // reference count is guaranteed to be 1 at this point, and we required
Packit f0b94e
            // the Arc itself to be `mut`, so we're returning the only possible
Packit f0b94e
            // reference to the inner data.
Packit f0b94e
            &mut (*this.ptr()).data
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized> Arc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn get_mut(this: &mut Self) -> Option<&mut T> {
Packit f0b94e
        if this.is_unique() {
Packit f0b94e
            unsafe {
Packit f0b94e
                // See make_mut() for documentation of the threadsafety here.
Packit f0b94e
                Some(&mut (*this.ptr()).data)
Packit f0b94e
            }
Packit f0b94e
        } else {
Packit f0b94e
            None
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn is_unique(&self) -> bool {
Packit f0b94e
        // We can use Relaxed here, but the justification is a bit subtle.
Packit f0b94e
        //
Packit f0b94e
        // The reason to use Acquire would be to synchronize with other threads
Packit f0b94e
        // that are modifying the refcount with Release, i.e. to ensure that
Packit f0b94e
        // their writes to memory guarded by this refcount are flushed. However,
Packit f0b94e
        // we know that threads only modify the contents of the Arc when they
Packit f0b94e
        // observe the refcount to be 1, and no other thread could observe that
Packit f0b94e
        // because we're holding one strong reference here.
Packit f0b94e
        self.inner().count.load(Relaxed) == 1
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized> Drop for Arc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn drop(&mut self) {
Packit f0b94e
        // Because `fetch_sub` is already atomic, we do not need to synchronize
Packit f0b94e
        // with other threads unless we are going to delete the object.
Packit f0b94e
        if self.inner().count.fetch_sub(1, Release) != 1 {
Packit f0b94e
            return;
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        // FIXME(bholley): Use the updated comment when [2] is merged.
Packit f0b94e
        //
Packit f0b94e
        // This load is needed to prevent reordering of use of the data and
Packit f0b94e
        // deletion of the data.  Because it is marked `Release`, the decreasing
Packit f0b94e
        // of the reference count synchronizes with this `Acquire` load. This
Packit f0b94e
        // means that use of the data happens before decreasing the reference
Packit f0b94e
        // count, which happens before this load, which happens before the
Packit f0b94e
        // deletion of the data.
Packit f0b94e
        //
Packit f0b94e
        // As explained in the [Boost documentation][1],
Packit f0b94e
        //
Packit f0b94e
        // > It is important to enforce any possible access to the object in one
Packit f0b94e
        // > thread (through an existing reference) to *happen before* deleting
Packit f0b94e
        // > the object in a different thread. This is achieved by a "release"
Packit f0b94e
        // > operation after dropping a reference (any access to the object
Packit f0b94e
        // > through this reference must obviously happened before), and an
Packit f0b94e
        // > "acquire" operation before deleting the object.
Packit f0b94e
        //
Packit f0b94e
        // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html)
Packit f0b94e
        // [2]: https://github.com/rust-lang/rust/pull/41714
Packit f0b94e
        self.inner().count.load(Acquire);
Packit f0b94e
Packit f0b94e
        unsafe {
Packit f0b94e
            self.drop_slow();
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
Packit f0b94e
    fn eq(&self, other: &Arc<T>) -> bool {
Packit f0b94e
        Self::ptr_eq(self, other) || *(*self) == *(*other)
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    fn ne(&self, other: &Arc<T>) -> bool {
Packit f0b94e
        !Self::ptr_eq(self, other) && *(*self) != *(*other)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
impl<T: ?Sized + PartialOrd> PartialOrd for Arc<T> {
Packit f0b94e
    fn partial_cmp(&self, other: &Arc<T>) -> Option<Ordering> {
Packit f0b94e
        (**self).partial_cmp(&**other)
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    fn lt(&self, other: &Arc<T>) -> bool {
Packit f0b94e
        *(*self) < *(*other)
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    fn le(&self, other: &Arc<T>) -> bool {
Packit f0b94e
        *(*self) <= *(*other)
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    fn gt(&self, other: &Arc<T>) -> bool {
Packit f0b94e
        *(*self) > *(*other)
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    fn ge(&self, other: &Arc<T>) -> bool {
Packit f0b94e
        *(*self) >= *(*other)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
impl<T: ?Sized + Ord> Ord for Arc<T> {
Packit f0b94e
    fn cmp(&self, other: &Arc<T>) -> Ordering {
Packit f0b94e
        (**self).cmp(&**other)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
impl<T: ?Sized + Eq> Eq for Arc<T> {}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized + fmt::Display> fmt::Display for Arc<T> {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        fmt::Display::fmt(&**self, f)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized + fmt::Debug> fmt::Debug for Arc<T> {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        fmt::Debug::fmt(&**self, f)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized> fmt::Pointer for Arc<T> {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        fmt::Pointer::fmt(&self.ptr(), f)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: Default> Default for Arc<T> {
Packit f0b94e
    fn default() -> Arc<T> {
Packit f0b94e
        Arc::new(Default::default())
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized + Hash> Hash for Arc<T> {
Packit f0b94e
    fn hash<H: Hasher>(&self, state: &mut H) {
Packit f0b94e
        (**self).hash(state)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T> From<T> for Arc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn from(t: T) -> Self {
Packit f0b94e
        Arc::new(t)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized> borrow::Borrow<T> for Arc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn borrow(&self) -> &T {
Packit f0b94e
        &**self
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: ?Sized> AsRef<T> for Arc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn as_ref(&self) -> &T {
Packit f0b94e
        &**self
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
unsafe impl<T: ?Sized> StableDeref for Arc<T> {}
Packit f0b94e
unsafe impl<T: ?Sized> CloneStableDeref for Arc<T> {}
Packit f0b94e
Packit f0b94e
#[cfg(feature = "servo")]
Packit f0b94e
impl<'de, T: Deserialize<'de>> Deserialize<'de> for Arc<T>
Packit f0b94e
{
Packit f0b94e
    fn deserialize<D>(deserializer: D) -> Result<Arc<T>, D::Error>
Packit f0b94e
    where
Packit f0b94e
        D: ::serde::de::Deserializer<'de>,
Packit f0b94e
    {
Packit f0b94e
        T::deserialize(deserializer).map(Arc::new)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
#[cfg(feature = "servo")]
Packit f0b94e
impl<T: Serialize> Serialize for Arc<T>
Packit f0b94e
{
Packit f0b94e
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
Packit f0b94e
    where
Packit f0b94e
        S: ::serde::ser::Serializer,
Packit f0b94e
    {
Packit f0b94e
        (**self).serialize(serializer)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// Structure to allow Arc-managing some fixed-sized data and a variably-sized
Packit f0b94e
/// slice in a single allocation.
Packit f0b94e
#[derive(Debug, Eq, PartialEq, PartialOrd)]
Packit f0b94e
pub struct HeaderSlice<H, T: ?Sized> {
Packit f0b94e
    /// The fixed-sized data.
Packit f0b94e
    pub header: H,
Packit f0b94e
Packit f0b94e
    /// The dynamically-sized data.
Packit f0b94e
    pub slice: T,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
#[inline(always)]
Packit f0b94e
fn divide_rounding_up(dividend: usize, divisor: usize) -> usize {
Packit f0b94e
    (dividend + divisor - 1) / divisor
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<H, T> Arc<HeaderSlice<H, [T]>> {
Packit f0b94e
    /// Creates an Arc for a HeaderSlice using the given header struct and
Packit f0b94e
    /// iterator to generate the slice. The resulting Arc will be fat.
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn from_header_and_iter(header: H, mut items: I) -> Self
Packit f0b94e
        where I: Iterator<Item=T> + ExactSizeIterator
Packit f0b94e
    {
Packit f0b94e
        use ::std::mem::size_of;
Packit f0b94e
        assert_ne!(size_of::<T>(), 0, "Need to think about ZST");
Packit f0b94e
Packit f0b94e
        // Compute the required size for the allocation.
Packit f0b94e
        let num_items = items.len();
Packit f0b94e
        let size = {
Packit f0b94e
            // First, determine the alignment of a hypothetical pointer to a
Packit f0b94e
            // HeaderSlice.
Packit f0b94e
            let fake_slice_ptr_align: usize = mem::align_of::<ArcInner<HeaderSlice<H, [T; 1]>>>();
Packit f0b94e
Packit f0b94e
            // Next, synthesize a totally garbage (but properly aligned) pointer
Packit f0b94e
            // to a sequence of T.
Packit f0b94e
            let fake_slice_ptr = fake_slice_ptr_align as *const T;
Packit f0b94e
Packit f0b94e
            // Convert that sequence to a fat pointer. The address component of
Packit f0b94e
            // the fat pointer will be garbage, but the length will be correct.
Packit f0b94e
            let fake_slice = unsafe { slice::from_raw_parts(fake_slice_ptr, num_items) };
Packit f0b94e
Packit f0b94e
            // Pretend the garbage address points to our allocation target (with
Packit f0b94e
            // a trailing sequence of T), rather than just a sequence of T.
Packit f0b94e
            let fake_ptr = fake_slice as *const [T] as *const ArcInner<HeaderSlice<H, [T]>>;
Packit f0b94e
            let fake_ref: &ArcInner<HeaderSlice<H, [T]>> = unsafe { &*fake_ptr };
Packit f0b94e
Packit f0b94e
            // Use size_of_val, which will combine static information about the
Packit f0b94e
            // type with the length from the fat pointer. The garbage address
Packit f0b94e
            // will not be used.
Packit f0b94e
            mem::size_of_val(fake_ref)
Packit f0b94e
        };
Packit f0b94e
Packit f0b94e
        let ptr: *mut ArcInner<HeaderSlice<H, [T]>>;
Packit f0b94e
        unsafe {
Packit f0b94e
            // Allocate the buffer. We use Vec because the underlying allocation
Packit f0b94e
            // machinery isn't available in stable Rust.
Packit f0b94e
            //
Packit f0b94e
            // To avoid alignment issues, we allocate words rather than bytes,
Packit f0b94e
            // rounding up to the nearest word size.
Packit f0b94e
            let buffer = if mem::align_of::<T>() <= mem::align_of::<usize>() {
Packit f0b94e
                Self::allocate_buffer::<usize>(size)
Packit f0b94e
            } else if mem::align_of::<T>() <= mem::align_of::<u64>() {
Packit f0b94e
                // On 32-bit platforms <T> may have 8 byte alignment while usize has 4 byte aligment.
Packit f0b94e
                // Use u64 to avoid over-alignment.
Packit f0b94e
                // This branch will compile away in optimized builds.
Packit f0b94e
                Self::allocate_buffer::<u64>(size)
Packit f0b94e
            } else {
Packit f0b94e
                panic!("Over-aligned type not handled");
Packit f0b94e
            };
Packit f0b94e
Packit f0b94e
            // Synthesize the fat pointer. We do this by claiming we have a direct
Packit f0b94e
            // pointer to a [T], and then changing the type of the borrow. The key
Packit f0b94e
            // point here is that the length portion of the fat pointer applies
Packit f0b94e
            // only to the number of elements in the dynamically-sized portion of
Packit f0b94e
            // the type, so the value will be the same whether it points to a [T]
Packit f0b94e
            // or something else with a [T] as its last member.
Packit f0b94e
            let fake_slice: &mut [T] = slice::from_raw_parts_mut(buffer as *mut T, num_items);
Packit f0b94e
            ptr = fake_slice as *mut [T] as *mut ArcInner<HeaderSlice<H, [T]>>;
Packit f0b94e
Packit f0b94e
            // Write the data.
Packit f0b94e
            //
Packit f0b94e
            // Note that any panics here (i.e. from the iterator) are safe, since
Packit f0b94e
            // we'll just leak the uninitialized memory.
Packit f0b94e
            ptr::write(&mut ((*ptr).count), atomic::AtomicUsize::new(1));
Packit f0b94e
            ptr::write(&mut ((*ptr).data.header), header);
Packit f0b94e
            let mut current: *mut T = &mut (*ptr).data.slice[0];
Packit f0b94e
            for _ in 0..num_items {
Packit f0b94e
                ptr::write(current, items.next().expect("ExactSizeIterator over-reported length"));
Packit f0b94e
                current = current.offset(1);
Packit f0b94e
            }
Packit f0b94e
            assert!(items.next().is_none(), "ExactSizeIterator under-reported length");
Packit f0b94e
Packit f0b94e
            // We should have consumed the buffer exactly.
Packit f0b94e
            debug_assert_eq!(current as *mut u8, buffer.offset(size as isize));
Packit f0b94e
        }
Packit f0b94e
Packit f0b94e
        // Return the fat Arc.
Packit f0b94e
        assert_eq!(size_of::<Self>(), size_of::<usize>() * 2, "The Arc will be fat");
Packit f0b94e
        Arc { p: NonZeroPtrMut::new(ptr) }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    #[inline]
Packit f0b94e
    unsafe fn allocate_buffer<W>(size: usize) -> *mut u8 {
Packit f0b94e
        let words_to_allocate = divide_rounding_up(size, mem::size_of::<W>());
Packit f0b94e
        let mut vec = Vec::<W>::with_capacity(words_to_allocate);
Packit f0b94e
        vec.set_len(words_to_allocate);
Packit f0b94e
        Box::into_raw(vec.into_boxed_slice()) as *mut W as *mut u8
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// Header data with an inline length. Consumers that use HeaderWithLength as the
Packit f0b94e
/// Header type in HeaderSlice can take advantage of ThinArc.
Packit f0b94e
#[derive(Debug, Eq, PartialEq, PartialOrd)]
Packit f0b94e
pub struct HeaderWithLength<H> {
Packit f0b94e
    /// The fixed-sized data.
Packit f0b94e
    pub header: H,
Packit f0b94e
Packit f0b94e
    /// The slice length.
Packit f0b94e
    length: usize,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<H> HeaderWithLength<H> {
Packit f0b94e
    /// Creates a new HeaderWithLength.
Packit f0b94e
    pub fn new(header: H, length: usize) -> Self {
Packit f0b94e
        HeaderWithLength {
Packit f0b94e
            header: header,
Packit f0b94e
            length: length,
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
type HeaderSliceWithLength<H, T> = HeaderSlice<HeaderWithLength<H>, T>;
Packit f0b94e
pub struct ThinArc<H: 'static, T: 'static> {
Packit f0b94e
    ptr: *mut ArcInner<HeaderSliceWithLength<H, [T; 1]>>,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
unsafe impl<H: Sync + Send, T: Sync + Send> Send for ThinArc<H, T> {}
Packit f0b94e
unsafe impl<H: Sync + Send, T: Sync + Send> Sync for ThinArc<H, T> {}
Packit f0b94e
Packit f0b94e
// Synthesize a fat pointer from a thin pointer.
Packit f0b94e
//
Packit f0b94e
// See the comment around the analogous operation in from_header_and_iter.
Packit f0b94e
fn thin_to_thick<H, T>(thin: *mut ArcInner<HeaderSliceWithLength<H, [T; 1]>>)
Packit f0b94e
    -> *mut ArcInner<HeaderSliceWithLength<H, [T]>>
Packit f0b94e
{
Packit f0b94e
    let len = unsafe { (*thin).data.header.length };
Packit f0b94e
    let fake_slice: *mut [T] = unsafe {
Packit f0b94e
        slice::from_raw_parts_mut(thin as *mut T, len)
Packit f0b94e
    };
Packit f0b94e
Packit f0b94e
    fake_slice as *mut ArcInner<HeaderSliceWithLength<H, [T]>>
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<H: 'static, T: 'static> ThinArc<H, T> {
Packit f0b94e
    /// Temporarily converts |self| into a bonafide Arc and exposes it to the
Packit f0b94e
    /// provided callback. The refcount is not modified.
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn with_arc<F, U>(&self, f: F) -> U
Packit f0b94e
        where F: FnOnce(&Arc<HeaderSliceWithLength<H, [T]>>) -> U
Packit f0b94e
    {
Packit f0b94e
        // Synthesize transient Arc, which never touches the refcount of the ArcInner.
Packit f0b94e
        let transient = NoDrop::new(Arc {
Packit f0b94e
            p: NonZeroPtrMut::new(thin_to_thick(self.ptr))
Packit f0b94e
        });
Packit f0b94e
Packit f0b94e
        // Expose the transient Arc to the callback, which may clone it if it wants.
Packit f0b94e
        let result = f(&transient);
Packit f0b94e
Packit f0b94e
        // Forget the transient Arc to leave the refcount untouched.
Packit f0b94e
        // XXXManishearth this can be removed when unions stabilize,
Packit f0b94e
        // since then NoDrop becomes zero overhead
Packit f0b94e
        mem::forget(transient);
Packit f0b94e
Packit f0b94e
        // Forward the result.
Packit f0b94e
        result
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Returns the address on the heap of the ThinArc itself -- not the T
Packit f0b94e
    /// within it -- for memory reporting.
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn heap_ptr(&self) -> *const c_void {
Packit f0b94e
        self.ptr as *const ArcInner<T> as *const c_void
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<H, T> Deref for ThinArc<H, T> {
Packit f0b94e
    type Target = HeaderSliceWithLength<H, [T]>;
Packit f0b94e
Packit f0b94e
    #[inline]
Packit f0b94e
    fn deref(&self) -> &Self::Target {
Packit f0b94e
        unsafe { &(*thin_to_thick(self.ptr)).data }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<H: 'static, T: 'static> Clone for ThinArc<H, T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn clone(&self) -> Self {
Packit f0b94e
        ThinArc::with_arc(self, |a| Arc::into_thin(a.clone()))
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<H: 'static, T: 'static> Drop for ThinArc<H, T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn drop(&mut self) {
Packit f0b94e
        let _ = Arc::from_thin(ThinArc { ptr: self.ptr });
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<H: 'static, T: 'static> Arc<HeaderSliceWithLength<H, [T]>> {
Packit f0b94e
    /// Converts an Arc into a ThinArc. This consumes the Arc, so the refcount
Packit f0b94e
    /// is not modified.
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn into_thin(a: Self) -> ThinArc<H, T> {
Packit f0b94e
        assert_eq!(a.header.length, a.slice.len(),
Packit f0b94e
                "Length needs to be correct for ThinArc to work");
Packit f0b94e
        let fat_ptr: *mut ArcInner<HeaderSliceWithLength<H, [T]>> = a.ptr();
Packit f0b94e
        mem::forget(a);
Packit f0b94e
        let thin_ptr = fat_ptr as *mut [usize] as *mut usize;
Packit f0b94e
        ThinArc {
Packit f0b94e
            ptr: thin_ptr as *mut ArcInner<HeaderSliceWithLength<H, [T; 1]>>
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Converts a ThinArc into an Arc. This consumes the ThinArc, so the refcount
Packit f0b94e
    /// is not modified.
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn from_thin(a: ThinArc<H, T>) -> Self {
Packit f0b94e
        let ptr = thin_to_thick(a.ptr);
Packit f0b94e
        mem::forget(a);
Packit f0b94e
        Arc {
Packit f0b94e
            p: NonZeroPtrMut::new(ptr)
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<H: PartialEq + 'static, T: PartialEq + 'static> PartialEq for ThinArc<H, T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn eq(&self, other: &ThinArc<H, T>) -> bool {
Packit f0b94e
        ThinArc::with_arc(self, |a| {
Packit f0b94e
            ThinArc::with_arc(other, |b| {
Packit f0b94e
                *a == *b
Packit f0b94e
            })
Packit f0b94e
        })
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<H: Eq + 'static, T: Eq + 'static> Eq for ThinArc<H, T> {}
Packit f0b94e
Packit f0b94e
/// An Arc, except it holds a pointer to the T instead of to the
Packit f0b94e
/// entire ArcInner.
Packit f0b94e
///
Packit f0b94e
/// ```text
Packit f0b94e
///  Arc<T>    RawOffsetArc<T>
Packit f0b94e
///   |          |
Packit f0b94e
///   v          v
Packit f0b94e
///  ---------------------
Packit f0b94e
/// | RefCount | T (data) | [ArcInner<T>]
Packit f0b94e
///  ---------------------
Packit f0b94e
/// ```
Packit f0b94e
///
Packit f0b94e
/// This means that this is a direct pointer to
Packit f0b94e
/// its contained data (and can be read from by both C++ and Rust),
Packit f0b94e
/// but we can also convert it to a "regular" Arc<T> by removing the offset
Packit f0b94e
#[derive(Eq)]
Packit f0b94e
#[repr(C)]
Packit f0b94e
pub struct RawOffsetArc<T: 'static> {
Packit f0b94e
    ptr: NonZeroPtrMut<T>,
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
unsafe impl<T: 'static + Sync + Send> Send for RawOffsetArc<T> {}
Packit f0b94e
unsafe impl<T: 'static + Sync + Send> Sync for RawOffsetArc<T> {}
Packit f0b94e
Packit f0b94e
impl<T: 'static> Deref for RawOffsetArc<T> {
Packit f0b94e
    type Target = T;
Packit f0b94e
    fn deref(&self) -> &Self::Target {
Packit f0b94e
        unsafe { &*self.ptr.ptr() }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: 'static> Clone for RawOffsetArc<T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn clone(&self) -> Self {
Packit f0b94e
        Arc::into_raw_offset(self.clone_arc())
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: 'static> Drop for RawOffsetArc<T> {
Packit f0b94e
    fn drop(&mut self) {
Packit f0b94e
        let _ = Arc::from_raw_offset(RawOffsetArc { ptr: self.ptr.clone() });
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
Packit f0b94e
impl<T: fmt::Debug + 'static> fmt::Debug for RawOffsetArc<T> {
Packit f0b94e
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Packit f0b94e
        fmt::Debug::fmt(&**self, f)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: PartialEq> PartialEq for RawOffsetArc<T> {
Packit f0b94e
    fn eq(&self, other: &RawOffsetArc<T>) -> bool {
Packit f0b94e
        *(*self) == *(*other)
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    fn ne(&self, other: &RawOffsetArc<T>) -> bool {
Packit f0b94e
        *(*self) != *(*other)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: 'static> RawOffsetArc<T> {
Packit f0b94e
    /// Temporarily converts |self| into a bonafide Arc and exposes it to the
Packit f0b94e
    /// provided callback. The refcount is not modified.
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn with_arc<F, U>(&self, f: F) -> U
Packit f0b94e
        where F: FnOnce(&Arc<T>) -> U
Packit f0b94e
    {
Packit f0b94e
        // Synthesize transient Arc, which never touches the refcount of the ArcInner.
Packit f0b94e
        let transient = unsafe { NoDrop::new(Arc::from_raw(self.ptr.ptr())) };
Packit f0b94e
Packit f0b94e
        // Expose the transient Arc to the callback, which may clone it if it wants.
Packit f0b94e
        let result = f(&transient);
Packit f0b94e
Packit f0b94e
        // Forget the transient Arc to leave the refcount untouched.
Packit f0b94e
        // XXXManishearth this can be removed when unions stabilize,
Packit f0b94e
        // since then NoDrop becomes zero overhead
Packit f0b94e
        mem::forget(transient);
Packit f0b94e
Packit f0b94e
        // Forward the result.
Packit f0b94e
        result
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// If uniquely owned, provide a mutable reference
Packit f0b94e
    /// Else create a copy, and mutate that
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn make_mut(&mut self) -> &mut T where T: Clone {
Packit f0b94e
        unsafe {
Packit f0b94e
            // extract the RawOffsetArc as an owned variable
Packit f0b94e
            let this = ptr::read(self);
Packit f0b94e
            // treat it as a real Arc
Packit f0b94e
            let mut arc = Arc::from_raw_offset(this);
Packit f0b94e
            // obtain the mutable reference. Cast away the lifetime
Packit f0b94e
            // This may mutate `arc`
Packit f0b94e
            let ret = Arc::make_mut(&mut arc) as *mut _;
Packit f0b94e
            // Store the possibly-mutated arc back inside, after converting
Packit f0b94e
            // it to a RawOffsetArc again
Packit f0b94e
            ptr::write(self, Arc::into_raw_offset(arc));
Packit f0b94e
            &mut *ret
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Clone it as an Arc
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn clone_arc(&self) -> Arc<T> {
Packit f0b94e
        RawOffsetArc::with_arc(self, |a| a.clone())
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Produce a pointer to the data that can be converted back
Packit f0b94e
    /// to an arc
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn borrow_arc<'a>(&'a self) -> ArcBorrow<'a, T> {
Packit f0b94e
        ArcBorrow(&**self)
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<T: 'static> Arc<T> {
Packit f0b94e
    /// Converts an Arc into a RawOffsetArc. This consumes the Arc, so the refcount
Packit f0b94e
    /// is not modified.
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn into_raw_offset(a: Self) -> RawOffsetArc<T> {
Packit f0b94e
        RawOffsetArc {
Packit f0b94e
            ptr: NonZeroPtrMut::new(Arc::into_raw(a) as *mut T),
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// Converts a RawOffsetArc into an Arc. This consumes the RawOffsetArc, so the refcount
Packit f0b94e
    /// is not modified.
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn from_raw_offset(a: RawOffsetArc<T>) -> Self {
Packit f0b94e
        let ptr = a.ptr.ptr();
Packit f0b94e
        mem::forget(a);
Packit f0b94e
        unsafe { Arc::from_raw(ptr) }
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
/// A "borrowed Arc". This is a pointer to
Packit f0b94e
/// a T that is known to have been allocated within an
Packit f0b94e
/// Arc.
Packit f0b94e
///
Packit f0b94e
/// This is equivalent in guarantees to `&Arc<T>`, however it is
Packit f0b94e
/// a bit more flexible. To obtain an `&Arc<T>` you must have
Packit f0b94e
/// an Arc<T> instance somewhere pinned down until we're done with it.
Packit f0b94e
///
Packit f0b94e
/// However, Gecko hands us refcounted things as pointers to T directly,
Packit f0b94e
/// so we have to conjure up a temporary Arc on the stack each time. The
Packit f0b94e
/// same happens for when the object is managed by a RawOffsetArc.
Packit f0b94e
///
Packit f0b94e
/// ArcBorrow lets us deal with borrows of known-refcounted objects
Packit f0b94e
/// without needing to worry about how they're actually stored.
Packit f0b94e
#[derive(Eq, PartialEq)]
Packit f0b94e
pub struct ArcBorrow<'a, T: 'a>(&'a T);
Packit f0b94e
Packit f0b94e
impl<'a, T> Copy for ArcBorrow<'a, T> {}
Packit f0b94e
impl<'a, T> Clone for ArcBorrow<'a, T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    fn clone(&self) -> Self {
Packit f0b94e
        *self
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<'a, T> ArcBorrow<'a, T> {
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn clone_arc(&self) -> Arc<T> {
Packit f0b94e
        let arc = unsafe { Arc::from_raw(self.0) };
Packit f0b94e
        // addref it!
Packit f0b94e
        mem::forget(arc.clone());
Packit f0b94e
        arc
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    /// For constructing from a reference known to be Arc-backed,
Packit f0b94e
    /// e.g. if we obtain such a reference over FFI
Packit f0b94e
    #[inline]
Packit f0b94e
    pub unsafe fn from_ref(r: &'a T) -> Self {
Packit f0b94e
        ArcBorrow(r)
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    #[inline]
Packit f0b94e
    pub fn with_arc<F, U>(&self, f: F) -> U where F: FnOnce(&Arc<T>) -> U, T: 'static {
Packit f0b94e
        // Synthesize transient Arc, which never touches the refcount.
Packit f0b94e
        let transient = unsafe { NoDrop::new(Arc::from_raw(self.0)) };
Packit f0b94e
Packit f0b94e
        // Expose the transient Arc to the callback, which may clone it if it wants.
Packit f0b94e
        let result = f(&transient);
Packit f0b94e
Packit f0b94e
        // Forget the transient Arc to leave the refcount untouched.
Packit f0b94e
        // XXXManishearth this can be removed when unions stabilize,
Packit f0b94e
        // since then NoDrop becomes zero overhead
Packit f0b94e
        mem::forget(transient);
Packit f0b94e
Packit f0b94e
        // Forward the result.
Packit f0b94e
        result
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
impl<'a, T> Deref for ArcBorrow<'a, T> {
Packit f0b94e
    type Target = T;
Packit f0b94e
Packit f0b94e
    #[inline]
Packit f0b94e
    fn deref(&self) -> &T {
Packit f0b94e
        &*self.0
Packit f0b94e
    }
Packit f0b94e
}
Packit f0b94e
Packit f0b94e
#[cfg(test)]
Packit f0b94e
mod tests {
Packit f0b94e
    use std::clone::Clone;
Packit f0b94e
    use std::ops::Drop;
Packit f0b94e
    use std::sync::atomic;
Packit f0b94e
    use std::sync::atomic::Ordering::{Acquire, SeqCst};
Packit f0b94e
    use super::{Arc, HeaderWithLength, ThinArc};
Packit f0b94e
Packit f0b94e
    #[derive(PartialEq)]
Packit f0b94e
    struct Canary(*mut atomic::AtomicUsize);
Packit f0b94e
Packit f0b94e
    impl Drop for Canary {
Packit f0b94e
        fn drop(&mut self) {
Packit f0b94e
            unsafe { (*self.0).fetch_add(1, SeqCst); }
Packit f0b94e
        }
Packit f0b94e
    }
Packit f0b94e
Packit f0b94e
    #[test]
Packit f0b94e
    fn slices_and_thin() {
Packit f0b94e
        let mut canary = atomic::AtomicUsize::new(0);
Packit f0b94e
        let c = Canary(&mut canary as *mut atomic::AtomicUsize);
Packit f0b94e
        let v = vec![5, 6];
Packit f0b94e
        let header = HeaderWithLength::new(c, v.len());
Packit f0b94e
        {
Packit f0b94e
            let x = Arc::into_thin(Arc::from_header_and_iter(header, v.into_iter()));
Packit f0b94e
            let y = ThinArc::with_arc(&x, |q| q.clone());
Packit f0b94e
            let _ = y.clone();
Packit f0b94e
            let _ = x == x;
Packit f0b94e
            Arc::from_thin(x.clone());
Packit f0b94e
        }
Packit f0b94e
        assert_eq!(canary.load(Acquire), 1);
Packit f0b94e
    }
Packit f0b94e
}