1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::inner_deref::InnerDerefMut;

/// An Observer is a consumer of values delivered by an Observable. One for each
/// type of notification delivered by the Observable: `next`, `error`,
/// and `complete`.
///
/// `Item` the type of the elements being emitted.
/// `Err`the type of the error may propagating.
pub trait Observer<Item, Err> {
  fn next(&mut self, value: Item);
  fn error(&mut self, err: Err);
  fn complete(&mut self);
  fn is_stopped(&self) -> bool;
}

#[doc(hidden)]
pub(crate) macro next_proxy_impl(
  $item: ident, $($name:tt $($parentheses:tt)?) .+)
{
  #[inline]
  fn next(&mut self, value: $item) {
    self.$($name$($parentheses)?).+.next(value);
  }
}

#[doc(hidden)]
pub(crate) macro error_proxy_impl(
  $err: ident, $($name:tt $($parentheses:tt)?) .+)
{
  #[inline]
  fn error(&mut self, err: $err) {
    self.$($name$($parentheses)?).+.error(err);
  }
}

#[doc(hidden)]
pub(crate) macro complete_proxy_impl($($name:tt $($parentheses:tt)?) .+) {
  #[inline]
  fn complete(&mut self) { self.$($name$($parentheses)?).+.complete(); }
}

#[doc(hidden)]
pub(crate) macro is_stopped_proxy_impl($($name:tt $($parentheses:tt)?) .+) {
  #[inline]
  fn is_stopped(&self) -> bool { self.$($name$($parentheses)?).+.is_stopped() }
}

impl<Item, Err, T> Observer<Item, Err> for T
where
  T: InnerDerefMut,
  T::Target: Observer<Item, Err>,
{
  fn next(&mut self, value: Item) { self.inner_deref_mut().next(value) }
  fn error(&mut self, err: Err) { self.inner_deref_mut().error(err); }
  fn complete(&mut self) { self.inner_deref_mut().complete(); }
  fn is_stopped(&self) -> bool { self.inner_deref().is_stopped() }
}