1use std::{ops::Deref, sync::Arc};
2
3pub struct State<T: ?Sized>(Arc<T>);
8
9impl<T> State<T> {
10 pub fn new(value: T) -> Self {
12 Self(Arc::new(value))
13 }
14}
15
16impl<T: ?Sized> State<T> {
17 pub const fn from_shared(value: Arc<T>) -> Self {
19 Self(value)
20 }
21
22 pub fn shared(&self) -> &Arc<T> {
24 &self.0
25 }
26
27 pub fn into_shared(self) -> Arc<T> {
29 self.0
30 }
31}
32
33impl<T: ?Sized> Clone for State<T> {
34 fn clone(&self) -> Self {
35 Self(self.0.clone())
36 }
37}
38
39impl<T: ?Sized> Deref for State<T> {
40 type Target = T;
41
42 fn deref(&self) -> &Self::Target {
43 &self.0
44 }
45}
46
47impl<T: ?Sized> std::fmt::Debug for State<T> {
48 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 formatter
50 .debug_tuple("State")
51 .field(&std::any::type_name::<T>())
52 .finish()
53 }
54}