use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
use super::atomic_ref::AtomicRef;
pub struct ArcAtomicRef<T> {
inner: Arc<AtomicRef<T>>,
}
impl<T> ArcAtomicRef<T> {
#[inline]
pub fn new(value: Arc<T>) -> Self {
Self::from_atomic_ref(AtomicRef::new(value))
}
#[inline]
pub fn from_value(value: T) -> Self {
Self::from_atomic_ref(AtomicRef::from_value(value))
}
#[inline]
pub fn from_atomic_ref(atomic_ref: AtomicRef<T>) -> Self {
Self {
inner: Arc::new(atomic_ref),
}
}
#[inline]
pub fn from_arc(inner: Arc<AtomicRef<T>>) -> Self {
Self { inner }
}
#[inline]
pub fn as_arc(&self) -> &Arc<AtomicRef<T>> {
&self.inner
}
#[inline]
pub fn into_arc(self) -> Arc<AtomicRef<T>> {
self.inner
}
#[inline]
pub fn strong_count(&self) -> usize {
Arc::strong_count(&self.inner)
}
}
impl<T> Clone for ArcAtomicRef<T> {
#[inline]
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
impl<T> Deref for ArcAtomicRef<T> {
type Target = AtomicRef<T>;
#[inline]
fn deref(&self) -> &Self::Target {
self.inner.as_ref()
}
}
impl<T> From<AtomicRef<T>> for ArcAtomicRef<T> {
#[inline]
fn from(atomic_ref: AtomicRef<T>) -> Self {
Self::from_atomic_ref(atomic_ref)
}
}
impl<T> From<Arc<AtomicRef<T>>> for ArcAtomicRef<T> {
#[inline]
fn from(inner: Arc<AtomicRef<T>>) -> Self {
Self::from_arc(inner)
}
}
impl<T: fmt::Debug> fmt::Debug for ArcAtomicRef<T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ArcAtomicRef")
.field("value", &self.load())
.field("strong_count", &self.strong_count())
.finish()
}
}
impl<T: fmt::Display> fmt::Display for ArcAtomicRef<T> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.load())
}
}