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
181
182
use crate::subscription::Publisher;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
pub trait Observer<Item, Err> {
fn next(&mut self, value: Item);
fn error(&mut self, err: Err);
fn complete(&mut self);
}
#[doc(hidden)]
pub(crate) macro observer_proxy_impl(
$ty: ty
, {$($name:tt $($parentheses:tt)?) .+}
, $item: ident
, $err: ident
$(, $host_ty: ident)? $(, <$($generics: tt),*>)?
$(, {where $($wty:ty : $bound: tt),*})?
) {
impl<$($($generics ,)*)? $item, $err, $($host_ty)? >
Observer<$item, $err> for $ty
where
$($host_ty: Observer<$item, $err>,)?
$($($wty: $bound), *)?
{
next_proxy_impl!($item, $($name$($parentheses)?).+);
error_proxy_impl!($err, $($name$($parentheses)?).+);
complete_proxy_impl!($($name$($parentheses)?).+);
}
}
#[doc(hidden)]
pub(crate) macro next_proxy_impl(
$item: ident, $($name:tt $($parentheses:tt)?) .+)
{
#[inline]
fn next(&mut self, value: $item) {
self.$($name$($parentheses)?).+.next(value);
}
}
#[doc(hidden)]
pub(crate) macro error_proxy_impl(
$err: ident, $($name:tt $($parentheses:tt)?) .+)
{
#[inline]
fn error(&mut self, err: $err) {
self.$($name$($parentheses)?).+.error(err);
}
}
#[doc(hidden)]
pub(crate) macro complete_proxy_impl($($name:tt $($parentheses:tt)?) .+) {
#[inline]
fn complete(&mut self) { self.$($name$($parentheses)?).+.complete(); }
}
#[doc(hidden)]
macro observer_pointer_proxy_impl(
$ty: ty, $item: ident, $err:ident $(, <$($generics: tt),*>)?)
{
impl<$($($generics ,)*)? $item, $err> Observer<$item, $err> for $ty {
#[inline]
fn next(&mut self, value: $item) { (&mut **self).next(value); }
#[inline]
fn error(&mut self, err: $err) { (&mut **self).error(err); }
#[inline]
fn complete(&mut self) { (&mut **self).complete(); }
}
}
observer_pointer_proxy_impl!(
Box<dyn Observer<Item, Err> + 'a>, Item, Err, <'a>);
observer_pointer_proxy_impl!(
Box<dyn Observer<Item, Err> + Send + Sync>,
Item,
Err
);
observer_pointer_proxy_impl!(
Box<dyn Publisher<Item, Err> + 'a>, Item, Err, <'a>);
observer_pointer_proxy_impl!(
Box<dyn Publisher<Item, Err> + Send + Sync>,
Item,
Err
);
observer_proxy_impl!(Arc<Mutex<S>>, { lock().unwrap() }, Item, Err, S);
observer_proxy_impl!(Rc<RefCell<S>>, { borrow_mut() }, Item, Err, S);
impl<Item, Err, T> Observer<Item, Err> for Vec<T>
where
T: Publisher<Item, Err>,
Item: PayloadCopy,
Err: PayloadCopy,
{
fn next(&mut self, value: Item) {
self.drain_filter(|subscriber| {
subscriber.next(value.payload_copy());
subscriber.is_closed()
});
}
fn error(&mut self, err: Err) {
self
.iter_mut()
.for_each(|subscriber| subscriber.error(err.payload_copy()));
self.clear();
}
fn complete(&mut self) {
self.iter_mut().for_each(|subscriber| subscriber.complete());
self.clear();
}
}
pub trait PayloadCopy: Clone {
#[must_use]
fn payload_copy(&self) -> Self { self.clone() }
}
impl<T: Copy> PayloadCopy for T {}