rxrust/
shared.rs

1use crate::prelude::*;
2
3/// Shared wrap the Observable, subscribe and accept subscribe in a safe mode
4/// by SharedObservable.
5#[derive(Clone)]
6pub struct Shared<R>(pub(crate) R);
7
8pub trait SharedObservable: Observable {
9  type Unsub: SubscriptionLike + 'static;
10  fn actual_subscribe<
11    O: Observer<Item = Self::Item, Err = Self::Err> + Sync + Send + 'static,
12  >(
13    self,
14    subscriber: Subscriber<O, SharedSubscription>,
15  ) -> Self::Unsub;
16
17  /// Convert to a thread-safe mode.
18  #[inline]
19  fn into_shared(self) -> Shared<Self>
20  where
21    Self: Sized,
22  {
23    Shared(self)
24  }
25}
26
27pub trait SharedEmitter: Emitter {
28  fn emit<O>(self, subscriber: Subscriber<O, SharedSubscription>)
29  where
30    O: Observer<Item = Self::Item, Err = Self::Err> + Send + Sync + 'static;
31}
32
33observable_proxy_impl!(Shared, S);
34
35impl<S> SharedObservable for Shared<S>
36where
37  S: SharedObservable,
38{
39  type Unsub = S::Unsub;
40  #[inline]
41  fn actual_subscribe<
42    O: Observer<Item = Self::Item, Err = Self::Err> + Sync + Send + 'static,
43  >(
44    self,
45    subscriber: Subscriber<O, SharedSubscription>,
46  ) -> Self::Unsub {
47    self.0.actual_subscribe(subscriber)
48  }
49}