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
use crate::impl_::router::Router as RouterImpl;
use crate::SodiumCtx;
use crate::Stream;
use std::hash::Hash;
/// Create a new Router that routes event items of type `A` to zero or
/// more [`Stream`]s of type `K` according to a given selector
/// function.
pub struct Router<A, K> {
impl_: RouterImpl<A, K>,
}
impl<A, K> Router<A, K> {
/// Create a new `Router` from the given input stream and selector
/// function.
pub fn new(
sodium_ctx: &SodiumCtx,
in_stream: &Stream<A>,
selector: impl Fn(&A) -> Vec<K> + Send + Sync + 'static,
) -> Router<A, K>
where
A: Clone + Send + 'static,
K: Send + Sync + Eq + Hash + 'static,
{
Router {
impl_: RouterImpl::new(&sodium_ctx.impl_, &in_stream.impl_, selector),
}
}
/// Create a Stream that is subscribed to event values that the
/// selector function routes to the given `K` value.
pub fn filter_matches(&self, k: &K) -> Stream<A>
where
A: Clone + Send + 'static,
K: Clone + Send + Sync + Eq + Hash + 'static,
{
Stream {
impl_: self.impl_.filter_matches(k),
}
}
}