rx_rust/operators/others/
map_infallible_to_error.rs1use crate::utils::types::NecessarySend;
2use crate::{
3 disposable::subscription::Subscription,
4 observable::Observable,
5 observer::{Observer, Termination},
6 utils::types::MarkerType,
7};
8use educe::Educe;
9use std::{convert::Infallible, marker::PhantomData};
10
11#[derive(Educe)]
37#[educe(Debug, Clone)]
38pub struct MapInfallibleToError<E, OE> {
39 source: OE,
40 _marker: MarkerType<E>,
41}
42
43impl<E, OE> MapInfallibleToError<E, OE> {
44 pub fn new(source: OE) -> Self {
45 Self {
46 source,
47 _marker: PhantomData,
48 }
49 }
50}
51
52impl<'or, 'sub, T, E, OE> Observable<'or, 'sub, T, E> for MapInfallibleToError<E, OE>
53where
54 E: 'or,
55 OE: Observable<'or, 'sub, T, Infallible>,
56{
57 fn subscribe(self, observer: impl Observer<T, E> + NecessarySend + 'or) -> Subscription<'sub> {
58 let observer = MapInfallibleToErrorObserver {
59 observer,
60 _marker: PhantomData,
61 };
62 self.source.subscribe(observer)
63 }
64}
65
66struct MapInfallibleToErrorObserver<E, OR> {
67 observer: OR,
68 _marker: MarkerType<E>,
69}
70
71impl<T, E, OR> Observer<T, Infallible> for MapInfallibleToErrorObserver<E, OR>
72where
73 OR: Observer<T, E>,
74{
75 fn on_next(&mut self, value: T) {
76 self.observer.on_next(value);
77 }
78
79 fn on_termination(self, termination: Termination<Infallible>) {
80 match termination {
81 Termination::Completed => self.observer.on_termination(Termination::Completed),
82 Termination::Error(_) => unreachable!(),
83 }
84 }
85}