atomptr 3.2.0

A safe copy-on-write wrapper around `AtomicPtr` with some extra tricks
Documentation
//! Reference snapshot handle

use std::ops::Deref;
use std::sync::Arc;

/// An alias for a referenced pointer
pub struct Ref<T> {
    pub(crate) inner: Box<Arc<T>>,
    pub(crate) ptr: *const Arc<T>,
}

impl<T> Ref<T> {
    pub fn as_ptr(&self) -> *const Arc<T> {
        self.ptr
    }

    /// Return the number of referrers to the underlying data
    pub fn count(&self) -> usize {
        Arc::strong_count(&*self.inner)
    }

    /// Consume this `Ref<T>` and grab the underlying `Arc<T>`
    ///
    /// Consuming a `Ref` removes your ability to `compare_exchange` new data into the underlying
    /// `AtomPtr` with a previously held data reference.  This function is different from
    /// `take_clone` (or `take_copy`) however in that it **does block** garbage collection.
    pub fn consume(self) -> Arc<T> {
	*self.inner
    }
}

impl<T: Copy> Ref<Option<T>> {
    /// Filter out references that internally contain `None`
    pub fn is_some(&self) -> bool {
        self.inner.is_some()
    }
}

impl<T: Copy> Ref<T> {
    /// Copy the underlying data without consuming the parent `Ref<T>`
    ///
    /// Multiple copies can be taken before garbage collection is released (when the `Ref<T>` goes
    /// out of scope).  Importantly copying the underlying data **does not block** gc.
    ///
    /// For non-`Copy` structures see [take_clone](Ref::take_clone)
    pub fn take_copy(&self) -> T {
        **self.inner.as_ref()
    }
}

impl<T: Clone> Ref<T> {
    /// Clone the underlying data structures without consuming the parent `Ref<T>`
    ///
    /// Multiple clones can be made before garbage collection is released (when the `Ref<T>` goes
    /// out of scope).  Importantly cloning the underlying data **does not block** gc.
    ///
    /// For `Copy` structures see [take_copy](Ref::take_copy)
    pub fn take_clone(&self) -> T {
        let that = (**self.inner.as_ref()).clone();
        that
    }
}

#[cfg(feature = "raw-deref")]
impl<T> Deref for Ref<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &**self.inner
    }
}

#[cfg(not(feature = "raw-deref"))]
impl<T> Deref for Ref<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &**self.inner
    }
}