rx_rust/observer/
callback_observer.rs1use super::{Flow, Observer, Termination};
2use educe::Educe;
3
4pub trait IntoFlow {
14 fn into_flow(self) -> Flow;
15}
16
17impl IntoFlow for () {
18 #[inline]
19 fn into_flow(self) -> Flow {
20 Flow::Continue
21 }
22}
23
24impl IntoFlow for Flow {
25 #[inline]
26 fn into_flow(self) -> Flow {
27 self
28 }
29}
30
31#[derive(Educe)]
32#[educe(Debug, Clone)]
33pub struct CallbackObserver<FN, FT> {
34 #[educe(Debug(ignore))]
35 on_next: FN,
36 #[educe(Debug(ignore))]
37 on_termination: FT,
38}
39
40impl<FN, FT> CallbackObserver<FN, FT> {
41 pub fn new<T, E, R>(on_next: FN, on_termination: FT) -> Self
42 where
43 FN: FnMut(T) -> R,
44 R: IntoFlow,
45 FT: FnOnce(Termination<E>),
46 {
47 Self {
48 on_next,
49 on_termination,
50 }
51 }
52}
53
54impl<T, E, R, FN, FT> Observer<T, E> for CallbackObserver<FN, FT>
55where
56 FN: FnMut(T) -> R,
57 R: IntoFlow,
58 FT: FnOnce(Termination<E>),
59{
60 fn on_next(&mut self, value: T) -> Flow {
61 (self.on_next)(value).into_flow()
64 }
65
66 fn on_termination(self, termination: Termination<E>) {
67 (self.on_termination)(termination);
68 }
69}