1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use std::{
ops::Deref,
sync::{Arc, Weak},
};
use crate::AsyncObservable;
#[derive(Clone)]
pub struct AsyncState<V>(Arc<AsyncObservable<V>>);
#[derive(Clone)]
pub struct WeakAsyncState<V>(Weak<AsyncObservable<V>>);
impl<V> AsyncState<V> {
pub fn new(v: V) -> Self {
Self(Arc::new(AsyncObservable::new(v)))
}
pub fn downgrade(&self) -> WeakAsyncState<V> {
WeakAsyncState(Arc::downgrade(&self.0))
}
}
impl<V> Deref for AsyncState<V> {
type Target = AsyncObservable<V>;
fn deref(&self) -> &Self::Target {
&*self.0
}
}
impl<V> Drop for AsyncState<V> {
fn drop(&mut self) {
(*self).notify();
}
}
impl<V> From<V> for AsyncState<V> {
fn from(v: V) -> Self {
Self::new(v)
}
}
impl<V> WeakAsyncState<V> {
pub fn upgrade(&self) -> AsyncState<V> {
AsyncState(self.0.upgrade().unwrap())
}
}