rx_rust/operators/creating/
range.rs

1use super::from_iter::FromIter;
2use crate::utils::types::NecessarySendSync;
3use crate::{disposable::subscription::Subscription, observable::Observable, observer::Observer};
4use educe::Educe;
5use std::{convert::Infallible, ops::RangeBounds};
6
7/// Creates an Observable that emits a sequence of integers within a specified range.
8/// See <https://reactivex.io/documentation/operators/range.html>
9///
10/// # Examples
11/// ```rust
12/// use rx_rust::{
13///     observable::observable_ext::ObservableExt,
14///     observer::Termination,
15///     operators::creating::range::Range,
16/// };
17///
18/// let mut values = Vec::new();
19/// let mut terminations = Vec::new();
20///
21/// Range::new(1..=3).subscribe_with_callback(
22///     |value| values.push(value),
23///     |termination| terminations.push(termination),
24/// );
25///
26/// assert_eq!(values, vec![1, 2, 3]);
27/// assert_eq!(terminations, vec![Termination::Completed]);
28/// ```
29#[derive(Educe)]
30#[educe(Debug, Clone)]
31pub struct Range<I>(I);
32
33impl<I> Range<I> {
34    pub fn new<T>(range: I) -> Self
35    where
36        I: IntoIterator<Item = T> + RangeBounds<T>,
37    {
38        Self(range)
39    }
40}
41
42impl<'or, 'sub, T, I> Observable<'or, 'sub, T, Infallible> for Range<I>
43where
44    I: IntoIterator<Item = T>,
45{
46    fn subscribe(
47        self,
48        observer: impl Observer<T, Infallible> + NecessarySendSync + 'or,
49    ) -> Subscription<'sub> {
50        FromIter::new(self.0).subscribe(observer)
51    }
52}