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
use futures_signals::signal::{Signal, SignalExt};
use std::marker::Unpin;
use std::pin::Pin;
use std::task::{Context, Poll};
pub struct DefaultSignal<S, T>
where
S: Signal<Item = T>,
{
default: Option<T>,
value_signal: Option<S>,
const_has_fired: bool,
}
impl<S, T> DefaultSignal<S, T>
where
S: Signal<Item = T>,
{
pub fn new(default: T, value_signal: Option<S>) -> Self {
Self {
default: Some(default),
value_signal,
const_has_fired: false,
}
}
}
impl<S, T> Signal for DefaultSignal<S, T>
where
S: Signal<Item = T> + Unpin,
T: Unpin,
{
type Item = T;
fn poll_change(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let _self = self.get_mut();
match &mut _self.value_signal {
None => {
if _self.const_has_fired {
Poll::Ready(None)
} else {
_self.const_has_fired = true;
Poll::Ready(_self.default.take())
}
}
Some(value_signal) => value_signal.poll_change_unpin(cx),
}
}
}
pub struct OptionSignal<S, T>
where
S: Signal<Item = T>,
{
value_signal: Option<S>,
const_has_fired: bool,
}
impl<S, T> OptionSignal<S, T>
where
S: Signal<Item = T>,
{
pub fn new(value_signal: Option<S>) -> Self {
Self {
value_signal,
const_has_fired: false,
}
}
}
impl<S, T> Signal for OptionSignal<S, T>
where
S: Signal<Item = T> + Unpin,
T: Unpin,
{
type Item = Option<T>;
fn poll_change(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
let _self = self.get_mut();
match &mut _self.value_signal {
None => {
if _self.const_has_fired {
Poll::Ready(None)
} else {
_self.const_has_fired = true;
Poll::Ready(Some(None))
}
}
Some(value_signal) => {
value_signal
.poll_change_unpin(cx)
.map(|value| value.map(|value| Some(value)))
}
}
}
}
pub enum EitherSignal<Left, Right> {
Left(Left),
Right(Right),
}
impl<Left, Right, T> Signal for EitherSignal<Left, Right>
where
Left: Signal<Item = T> + Unpin,
Right: Signal<Item = T> + Unpin,
{
type Item = T;
fn poll_change(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
match self.get_mut() {
Self::Left(x) => x.poll_change_unpin(cx),
Self::Right(x) => x.poll_change_unpin(cx),
}
}
}