use std::{ops::Deref, sync::Arc};
pub struct State<T: ?Sized>(Arc<T>);
impl<T> State<T> {
pub fn new(value: T) -> Self {
Self(Arc::new(value))
}
}
impl<T: ?Sized> State<T> {
pub const fn from_shared(value: Arc<T>) -> Self {
Self(value)
}
pub fn shared(&self) -> &Arc<T> {
&self.0
}
pub fn into_shared(self) -> Arc<T> {
self.0
}
}
impl<T: ?Sized> Clone for State<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T: ?Sized> Deref for State<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: ?Sized> std::fmt::Debug for State<T> {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_tuple("State")
.field(&std::any::type_name::<T>())
.finish()
}
}