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