rx_rust/operators/combining/
start_with.rs

1use crate::utils::types::NecessarySendSync;
2use crate::{disposable::subscription::Subscription, observable::Observable, observer::Observer};
3use educe::Educe;
4
5/// Emits a specified sequence of values before beginning to emit the items from the source Observable.
6/// See <https://reactivex.io/documentation/operators/startwith.html>
7///
8/// # Examples
9/// ```rust
10/// use rx_rust::{
11///     observable::observable_ext::ObservableExt,
12///     observer::Termination,
13///     operators::{
14///         combining::start_with::StartWith,
15///         creating::from_iter::FromIter,
16///     },
17/// };
18///
19/// let mut values = Vec::new();
20/// let mut terminations = Vec::new();
21///
22/// let observable = StartWith::new(FromIter::new(vec![3, 4]), vec![1, 2]);
23/// observable.subscribe_with_callback(
24///     |value| values.push(value),
25///     |termination| terminations.push(termination),
26/// );
27///
28/// assert_eq!(values, vec![1, 2, 3, 4]);
29/// assert_eq!(terminations, vec![Termination::Completed]);
30/// ```
31#[derive(Educe)]
32#[educe(Debug, Clone)]
33pub struct StartWith<OE, I> {
34    source: OE,
35    values: I,
36}
37
38impl<OE, I> StartWith<OE, I> {
39    pub fn new<'or, 'sub, T, E>(source: OE, values: I) -> Self
40    where
41        OE: Observable<'or, 'sub, T, E>,
42        I: IntoIterator<Item = T>,
43    {
44        Self { source, values }
45    }
46}
47
48impl<'or, 'sub, T, E, OE, I> Observable<'or, 'sub, T, E> for StartWith<OE, I>
49where
50    OE: Observable<'or, 'sub, T, E>,
51    I: IntoIterator<Item = T>,
52{
53    fn subscribe(
54        self,
55        mut observer: impl Observer<T, E> + NecessarySendSync + 'or,
56    ) -> Subscription<'sub> {
57        for value in self.values.into_iter() {
58            observer.on_next(value);
59        }
60        self.source.subscribe(observer)
61    }
62}