Skip to main content

rx_rust/operators/creating/
empty.rs

1use crate::utils::types::MaybeSend;
2use crate::{
3    observable::{Observable, Subscription},
4    observer::{Observer, Termination},
5};
6use std::convert::Infallible;
7
8/// Creates an Observable that emits no items and then terminates normally.
9/// See <https://reactivex.io/documentation/operators/empty-never-throw.html>
10///
11/// # Examples
12/// ```rust
13/// use rx_rust::{
14///     observable::ObservableExt,
15///     observer::Termination,
16///     operators::creating::empty::Empty,
17/// };
18/// use std::convert::Infallible;
19///
20/// let mut terminations = Vec::new();
21///
22/// Empty.subscribe_with_callback(
23///     |_: Infallible| -> () { unreachable!() },
24///     |termination| terminations.push(termination),
25/// );
26///
27/// assert_eq!(terminations, vec![Termination::Completed]);
28/// ```
29#[derive(Debug, Clone)]
30pub struct Empty;
31
32impl<'or> Observable<'or, Infallible, Infallible> for Empty {
33    type D = ();
34
35    fn subscribe(
36        self,
37        observer: impl Observer<Infallible, Infallible> + MaybeSend + 'or,
38    ) -> Subscription<Self::D> {
39        observer.on_termination(Termination::Completed);
40        Subscription::default()
41    }
42}