Macro rxrust::impl_local_shared_both[][src]

macro_rules! impl_local_shared_both {
    (@ replace $(+ $i : literal) *, $ctx : ident, [$($before : tt) *] macro
 method($($args : tt) *) $body : tt $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, impl_local, [$($before : tt) *] @ ctx ::
 Observable < $($ie : ident = $b : ty), + > $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, impl_local, [$($before : tt) *] @ ctx ::
 Observable $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, impl_shared, [$($before : tt) *] @ ctx ::
 Observable $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, impl_local, [$($before : tt) *] @ ctx ::
 shared_only($($b : tt) *) $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, impl_shared, [$($before : tt) *] @ ctx ::
 shared_only($($b : tt) *) $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, impl_local, [$($before : tt) *] @ ctx ::
 local_only($($b : tt) *) $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, impl_shared, [$($before : tt) *] @ ctx ::
 local_only($($b : tt) *) $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, $ctx : ident, [$($before : tt) *] @ ctx
 $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, $ctx : ident, [$($before : tt) *]
 ($arg1 : ty, $($args : ty), +) $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, $ctx : ident, [$($before : tt) *]
 ($($caller : tt) +) $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, $ctx : ident, [$($before : tt) *]
 [$($caller : tt) +] $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, $ctx : ident, [$($before : tt) *]
 { $($caller : tt) + } $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) *, $ctx : ident, [$($before : tt) *] $caller : tt
 $($t : tt) *) => { ... };
    (@ replace $(+ $i : literal) +, $ctx : ident, [$($before : tt) *]) => { ... };
    (@ replace, $ctx : ident, [$($before : tt) *]) => { ... };
    (@ impl_local, impl $(< $($ilf : lifetime,) * $($ig : ident), * >) ?
 $($t : ident) :: + $(< $($lf : lifetime,) * $($g : ty), * >) ? ; type Unsub =
 $u : ty ; macro method($($args : tt) *) $body : tt $(where $($b : tt) +) ?) => { ... };
    (@ impl_shared, impl $(< $($ilf : lifetime,) * $($ig : ident), * >) ?
 $($t : ident) :: + $(< $($lf : lifetime,) * $($g : ty), * >) ? ; type Unsub =
 $u : ty ; macro method($($args : tt) *) $body : tt $(where $($b : tt) +) ?) => { ... };
    ($($t : tt) *) => { ... };
}
Expand description

impl_local_shared_both is a macro helper to implement both the LocalObservable and SharedObservable at once. You can use @ctx to unify the same meaning but different types in local and shared implementation. See (local)[local] and (‘shared’)[shared] mod to learn what type you can use. Define an method macro use to write the method code of the LocalObservable and SharedObservable. This macro should named with method and always accept three arguments are $self $observer and $ctx. $self and $observer are the arguments of the actual_subscribe method. $ctx is either impl_local mod or impl_shared.

Here is the schema of the macro.

impl_local_shared_both! {
  // First part is tell the macro what type you want implement for and the
  // generics use to implement it.
  impl$(<..>)? T$(<..>)?;
  // This line specify the associtated type `Unsub`.
  type Unsub = $ty;
  // The macro use to implement the body of the method.
  macro method($self: ident, $observer: ident, $ctx: ident) {
    // write your `actual_subscribe` implementation in this block.
  }
  // if your implementation has some bounds, please provide a where clause
  where $ident:$ty,
}

Here is a real example which is the implementation of ObservableFn.

impl_local_shared_both! {
  impl<F, Item, Err> ObservableFn<F, Item, Err>;
  type Unsub = SingleSubscription;
  macro method($self: ident, $observer: ident, $ctx: ident) {
    ($self.0)(&mut $observer);
    SingleSubscription::default()
  }
  where F: FnOnce(&mut dyn Observer<Item = Item, Err = Err>)
}