atomptr 3.2.0

A safe copy-on-write wrapper around `AtomicPtr` with some extra tricks
Documentation
use crate::Ref;

/// Result from a CAS operation
pub enum CasResult<T> {
    Success(Ref<T>),
    Failure(Ref<T>),
}

impl<T> CasResult<T> {
    pub fn is_success(&self) -> bool {
        match self {
            Self::Success(_) => true,
            Self::Failure(_) => false,
        }
    }

    /// Return the inner `Ref<T>` only if the CAS operation was a
    /// success, else panic
    pub fn unwrap_ref(&self) -> &Ref<T> {
        match self {
            Self::Success(r) => r,
            _ => panic!("called `unwrap()` on a failed CAS operation result"),
        }
    }

    pub fn ok_or<I, E>(&self, err: I) -> Result<&Ref<T>, E>
    where
        I: Into<E>,
        E: std::error::Error,
    {
        match self {
            Self::Success(r) => Ok(r),
            Self::Failure(_) => Err(err.into()),
        }
    }

    pub fn inner(self) -> Ref<T> {
        match self {
            Self::Success(r) => r,
            Self::Failure(r) => r,
        }
    }

    /// Expose atomic operation state as optional previous value (in
    /// case of success), None if it failed
    pub fn success(&self) -> Option<&T> {
        match self {
            Self::Success(r) => Some(&r.inner.as_ref()),
            _ => None,
        }
    }
}