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