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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use crate::{event::Event, solver::SolveError, thread::TerminationHandle};
use futures::Future;
use std::{
fmt::Debug,
pin::Pin,
sync::{Arc, Mutex},
task::{Poll, Waker},
};
#[derive(derivative::Derivative)]
#[derivative(Clone, Debug, Default(bound = ""), PartialEq, Eq)]
pub enum State<T> {
#[derivative(Default)]
Pending,
Ready(Arc<T>),
Error(Vec<SolveError>),
}
pub type EventCallback<T, E> = Arc<Mutex<dyn Fn(Event<'_, T, E>) + Send>>;
#[derive(derivative::Derivative)]
#[derivative(Debug = "transparent", Default(bound = ""), PartialEq, Eq)]
pub struct ActivationInner<T> {
state: State<T>,
#[derivative(Debug = "ignore", Default(value = "None"), PartialEq = "ignore")]
waker: Option<Waker>,
}
impl<T> From<T> for ActivationInner<T> {
fn from(value: T) -> Self {
Self {
state: State::Ready(Arc::new(value)),
waker: None,
}
}
}
impl<T> ActivationInner<T> {
pub fn new() -> Self {
Self::default()
}
pub fn state(&self) -> &State<T> {
&self.state
}
pub fn set_pending(&mut self) {
self.state = State::Pending;
}
pub fn set_value(&mut self, value: T) {
self.state = State::Ready(Arc::new(value));
self.wake();
}
pub fn set_value_arc(&mut self, value: Arc<T>) {
self.state = State::Ready(value);
self.wake();
}
pub fn set_error(&mut self, errors: Vec<SolveError>) {
if let State::Error(previous_errors) = &mut self.state {
previous_errors.extend(errors);
} else {
self.state = State::Error(errors)
}
self.wake();
}
fn wake(&mut self) {
if let Some(waker) = self.waker.take() {
waker.wake();
}
}
}
pub struct Activation<T> {
pub inner: Arc<Mutex<ActivationInner<T>>>,
pub producer: Option<TerminationHandle>,
}
impl<T> Clone for Activation<T> {
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
producer: self.producer.clone(),
}
}
}
impl<T> Activation<T> {
pub fn inner(&self) -> &Arc<Mutex<ActivationInner<T>>> {
&self.inner
}
pub fn cancel(&mut self, e: SolveError) {
let mut inner = self.inner.lock().unwrap();
if let State::Pending = inner.state {
inner.state = State::Error(vec![e]);
}
self.producer = None;
}
}
impl<T: Debug> Debug for Activation<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let shared_state = self.inner.lock().expect("Could not lock shared_state");
write!(f, "Activation({:?})", shared_state)
}
}
impl<T> From<T> for Activation<T> {
fn from(value: T) -> Self {
Self {
inner: Arc::new(Mutex::new(ActivationInner::from(value))),
producer: None,
}
}
}
pub type Value<T> = Result<Arc<T>, Vec<SolveError>>;
impl<T> Future for Activation<T> {
type Output = Value<T>;
fn poll(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<Self::Output> {
let mut inner = self.inner.lock().unwrap();
match &inner.state {
State::Pending => {
inner.waker = Some(cx.waker().clone());
Poll::Pending
}
State::Ready(value) => Poll::Ready(Ok(Arc::clone(value))),
State::Error(errors) => Poll::Ready(Err(errors.clone())),
}
}
}
impl<T: PartialEq> PartialEq for Activation<T> {
fn eq(&self, other: &Self) -> bool {
let v1 = self.inner.lock().expect("Coult not lock st1");
let v2 = other.inner.lock().expect("Coult not lock st2");
*v1 == *v2
}
}