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