use std::fmt;
use std::ops::Deref;
use std::sync::Arc;
use super::atomic_signed_count::AtomicSignedCount;
pub struct ArcAtomicSignedCount {
inner: Arc<AtomicSignedCount>,
}
impl ArcAtomicSignedCount {
#[inline]
pub fn new(value: isize) -> Self {
Self::from_count(AtomicSignedCount::new(value))
}
#[inline]
pub fn zero() -> Self {
Self::new(0)
}
#[inline]
pub fn from_count(counter: AtomicSignedCount) -> Self {
Self {
inner: Arc::new(counter),
}
}
#[inline]
pub fn from_arc(inner: Arc<AtomicSignedCount>) -> Self {
Self { inner }
}
#[inline]
pub fn as_arc(&self) -> &Arc<AtomicSignedCount> {
&self.inner
}
#[inline]
pub fn into_arc(self) -> Arc<AtomicSignedCount> {
self.inner
}
#[inline]
pub fn strong_count(&self) -> usize {
Arc::strong_count(&self.inner)
}
}
impl Clone for ArcAtomicSignedCount {
#[inline]
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
}
}
}
impl Default for ArcAtomicSignedCount {
#[inline]
fn default() -> Self {
Self::zero()
}
}
impl Deref for ArcAtomicSignedCount {
type Target = AtomicSignedCount;
#[inline]
fn deref(&self) -> &Self::Target {
self.inner.as_ref()
}
}
impl From<isize> for ArcAtomicSignedCount {
#[inline]
fn from(value: isize) -> Self {
Self::new(value)
}
}
impl From<AtomicSignedCount> for ArcAtomicSignedCount {
#[inline]
fn from(counter: AtomicSignedCount) -> Self {
Self::from_count(counter)
}
}
impl From<Arc<AtomicSignedCount>> for ArcAtomicSignedCount {
#[inline]
fn from(inner: Arc<AtomicSignedCount>) -> Self {
Self::from_arc(inner)
}
}
impl fmt::Debug for ArcAtomicSignedCount {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ArcAtomicSignedCount")
.field("value", &self.get())
.field("strong_count", &self.strong_count())
.finish()
}
}
impl fmt::Display for ArcAtomicSignedCount {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.get())
}
}