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
use crate::prelude::*;
#[derive(Clone)]
pub struct ObserverAll<N, E, C> {
next: N,
error: E,
complete: C,
is_stopped: bool,
}
impl<N, E, C> ObserverAll<N, E, C> {
#[inline(always)]
pub fn new(next: N, error: E, complete: C) -> Self {
ObserverAll {
next,
error,
complete,
is_stopped: false,
}
}
}
impl<Item, Err, N, E, C> Observer<Item, Err> for ObserverAll<N, E, C>
where
C: FnMut(),
N: FnMut(Item),
E: FnMut(Err),
{
#[inline(always)]
fn next(&mut self, value: Item) { (self.next)(value); }
fn error(&mut self, err: Err) {
(self.error)(err);
self.is_stopped = true;
}
fn complete(&mut self) {
(self.complete)();
self.is_stopped = true;
}
#[inline]
fn is_stopped(&self) -> bool { self.is_stopped }
}
pub trait SubscribeAll<'a, N, E, C> {
type Unsub: SubscriptionLike;
fn subscribe_all(
self,
next: N,
error: E,
complete: C,
) -> SubscriptionWrapper<Self::Unsub>;
}
impl<'a, S, N, E, C> SubscribeAll<'a, N, E, C> for S
where
S: LocalObservable<'a>,
N: FnMut(S::Item) + 'a,
E: FnMut(S::Err) + 'a,
C: FnMut() + 'a,
{
type Unsub = S::Unsub;
fn subscribe_all(
self,
next: N,
error: E,
complete: C,
) -> SubscriptionWrapper<Self::Unsub>
where
Self: Sized,
{
let subscriber = Subscriber::local(ObserverAll {
next,
error,
complete,
is_stopped: false,
});
SubscriptionWrapper(self.actual_subscribe(subscriber))
}
}
impl<'a, S, N, E, C> SubscribeAll<'a, N, E, C> for Shared<S>
where
S: SharedObservable,
N: FnMut(S::Item) + Send + Sync + 'static,
E: FnMut(S::Err) + Send + Sync + 'static,
C: FnMut() + Send + Sync + 'static,
{
type Unsub = S::Unsub;
fn subscribe_all(
self,
next: N,
error: E,
complete: C,
) -> SubscriptionWrapper<Self::Unsub>
where
Self: Sized,
{
let subscriber = Subscriber::shared(ObserverAll {
next,
error,
complete,
is_stopped: false,
});
SubscriptionWrapper(self.0.actual_subscribe(subscriber))
}
}
#[test]
fn raii() {
let mut times = 0;
{
let mut subject = Subject::new();
{
let _ = subject
.clone()
.subscribe_all(|_| times += 1, |_| {}, || {})
.unsubscribe_when_dropped();
}
subject.next(());
subject.error(());
}
assert_eq!(times, 0);
}