use std::{
fmt::{Debug, Formatter},
hash::{Hash, Hasher},
ops::Deref,
sync::Arc,
};
pub enum SArc<T: 'static + ?Sized> {
Static(&'static T),
Arc(Arc<T>),
}
impl<T: 'static + ?Sized> Deref for SArc<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
match self {
SArc::Static(st) => st,
SArc::Arc(arc) => arc,
}
}
}
impl<T: 'static + ?Sized + Hash> Hash for SArc<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.deref().hash(state)
}
}
impl<T: 'static + ?Sized + PartialEq> PartialEq for SArc<T> {
fn eq(&self, other: &Self) -> bool {
*self.deref() == *other.deref()
}
}
impl<T: 'static + ?Sized + Eq> Eq for SArc<T> {}
impl<T: 'static + ?Sized> From<Box<T>> for SArc<T> {
fn from(value: Box<T>) -> Self {
SArc::Arc(Arc::from(value))
}
}
impl<T: 'static + Default> Default for SArc<T> {
fn default() -> Self {
SArc::Arc(Arc::default())
}
}
impl<T: 'static> Default for SArc<[T]> {
fn default() -> Self {
SArc::Arc(Arc::default())
}
}
impl<T: 'static + ?Sized> Clone for SArc<T> {
fn clone(&self) -> Self {
match self {
SArc::Static(st) => SArc::Static(st),
SArc::Arc(arc) => SArc::Arc(arc.clone()),
}
}
}
impl<T: Debug> Debug for SArc<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", **self)
}
}
impl<T: 'static> SArc<T> {
pub fn new(value: T) -> SArc<T> {
SArc::Arc(Arc::new(value))
}
}