rx_rust/operators/creating/throw.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 terminates with an error.
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::throw::Throw,
19/// };
20/// use std::convert::Infallible;
21///
22/// let mut terminations = Vec::new();
23///
24/// Throw::new("boom").subscribe_with_callback(
25/// |value: Infallible| panic!("`Throw` should not emit values"),
26/// |termination| terminations.push(termination),
27/// );
28///
29/// assert_eq!(terminations, vec![Termination::Error("boom")]);
30/// ```
31#[derive(Educe)]
32#[educe(Debug, Clone)]
33pub struct Throw<E>(E);
34
35impl<E> Throw<E> {
36 pub fn new(error: E) -> Self {
37 Self(error)
38 }
39}
40
41impl<'or, 'sub, E> Observable<'or, 'sub, Infallible, E> for Throw<E> {
42 fn subscribe(
43 self,
44 observer: impl Observer<Infallible, E> + NecessarySendSync + 'or,
45 ) -> Subscription<'sub> {
46 observer.on_termination(Termination::Error(self.0));
47 Subscription::default()
48 }
49}