rx_rust/operators/conditional_boolean/
take_while.rs1use crate::utils::types::NecessarySendSync;
2use crate::utils::unsub_after_termination::subscribe_unsub_after_termination;
3use crate::{
4 disposable::subscription::Subscription,
5 observable::Observable,
6 observer::{Observer, Termination},
7};
8use educe::Educe;
9
10#[derive(Educe)]
37#[educe(Debug, Clone)]
38pub struct TakeWhile<OE, F> {
39 source: OE,
40 callback: F,
41}
42
43impl<OE, F> TakeWhile<OE, F> {
44 pub fn new<'or, 'sub, T, E>(source: OE, callback: F) -> Self
45 where
46 OE: Observable<'or, 'sub, T, E>,
47 F: FnMut(&T) -> bool,
48 {
49 Self { source, callback }
50 }
51}
52
53impl<'or, 'sub, T, E, OE, F> Observable<'or, 'sub, T, E> for TakeWhile<OE, F>
54where
55 OE: Observable<'or, 'sub, T, E>,
56 F: FnMut(&T) -> bool + NecessarySendSync + 'or,
57 'sub: 'or,
58{
59 fn subscribe(
60 self,
61 observer: impl Observer<T, E> + NecessarySendSync + 'or,
62 ) -> Subscription<'sub> {
63 subscribe_unsub_after_termination(observer, |observer| {
64 let observer = TakeWhileObserver {
65 observer: Some(observer),
66 callback: self.callback,
67 };
68 self.source.subscribe(observer)
69 })
70 }
71}
72
73struct TakeWhileObserver<OR, F> {
74 observer: Option<OR>,
75 callback: F,
76}
77
78impl<T, E, OR, F> Observer<T, E> for TakeWhileObserver<OR, F>
79where
80 OR: Observer<T, E>,
81 F: FnMut(&T) -> bool,
82{
83 fn on_next(&mut self, value: T) {
84 let r#continue = (self.callback)(&value);
85 if r#continue {
86 if let Some(observer) = self.observer.as_mut() {
87 observer.on_next(value);
88 }
89 } else if let Some(observer) = self.observer.take() {
90 observer.on_termination(Termination::Completed);
91 }
92 }
93
94 fn on_termination(self, termination: Termination<E>) {
95 if let Some(observer) = self.observer {
96 observer.on_termination(termination);
97 }
98 }
99}