rx_rust/operators/conditional_boolean/
all.rs1use crate::observable::Subscription;
2use crate::utils::subscribe_with_auto_dispose_on_termination::{
3 self, subscribe_with_auto_dispose_on_termination,
4};
5use crate::utils::types::{MarkerType, MaybeSend};
6use crate::{
7 observable::Observable,
8 observer::{Flow, Observer, Termination},
9};
10use educe::Educe;
11use std::marker::PhantomData;
12
13#[derive(Educe)]
40#[educe(Debug, Clone)]
41pub struct All<T, OE, F> {
42 source: OE,
43 callback: F,
44 _marker: MarkerType<T>,
45}
46
47impl<T, OE, F> All<T, OE, F> {
48 pub fn new<'or, E>(source: OE, callback: F) -> Self
49 where
50 OE: Observable<'or, T, E>,
51 F: FnMut(T) -> bool,
52 {
53 Self {
54 source,
55 callback,
56 _marker: PhantomData,
57 }
58 }
59}
60
61impl<'or, T, E, OE, F> Observable<'or, bool, E> for All<T, OE, F>
62where
63 OE: Observable<'or, T, E>,
64 OE::D: MaybeSend + 'or,
65 F: FnMut(T) -> bool + MaybeSend + 'or,
66{
67 type D = subscribe_with_auto_dispose_on_termination::Disposal<OE::D>;
68
69 fn subscribe(
70 self,
71 observer: impl Observer<bool, E> + MaybeSend + 'or,
72 ) -> Subscription<Self::D> {
73 subscribe_with_auto_dispose_on_termination(observer, |observer| {
74 let observer = AllObserver {
75 observer: Some(observer),
76 callback: self.callback,
77 };
78 self.source.subscribe(observer)
79 })
80 }
81}
82
83struct AllObserver<OR, F> {
84 observer: Option<OR>,
85 callback: F,
86}
87
88impl<T, E, OR, F> Observer<T, E> for AllObserver<OR, F>
89where
90 OR: Observer<bool, E>,
91 F: FnMut(T) -> bool,
92{
93 fn on_next(&mut self, value: T) -> Flow {
94 if self.observer.is_none() {
97 return Flow::Stop;
98 }
99 if (self.callback)(value) {
100 return Flow::Continue;
101 }
102 let Some(mut observer) = self.observer.take() else {
104 return Flow::Stop;
105 };
106 if observer.on_next(false).is_continue() {
107 observer.on_termination(Termination::Completed);
108 }
109 Flow::Stop
110 }
111
112 fn on_termination(mut self, termination: Termination<E>) {
113 if let Some(mut observer) = self.observer.take() {
114 match termination {
115 Termination::Completed => drop(observer.on_next(true)),
116 Termination::Error(_) => {}
117 }
118 observer.on_termination(termination);
119 }
120 }
121}