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