use std::ops::Deref;
use std::sync::Arc;
use std::hash::{Hash, Hasher};
use std::fmt;
use serde::{Deserialize, Serialize};
#[derive(Clone,Serialize,Deserialize)]
pub struct ArcPtr<T>(pub Arc<T>);
impl<T> PartialEq for ArcPtr<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
impl<T> Eq for ArcPtr<T> {}
impl<T> Hash for ArcPtr<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
(Arc::as_ptr(&self.0) as *const () as usize).hash(state);
}
}
impl<T> Deref for ArcPtr<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: fmt::Debug> fmt::Debug for ArcPtr<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ArcPtr")
.field("ptr", &Arc::as_ptr(&self.0))
.field("data", &self.0)
.finish()
}
}
impl<T> ArcPtr<T> {
pub fn new(inner: T) -> Self {
Self(Arc::new(inner))
}
}