sodium_rust/
router.rs

1use crate::impl_::router::Router as RouterImpl;
2use crate::SodiumCtx;
3use crate::Stream;
4use std::hash::Hash;
5
6/// Create a new Router that routes event items of type `A` to zero or
7/// more [`Stream`]s of type `K` according to a given selector
8/// function.
9pub struct Router<A, K> {
10    impl_: RouterImpl<A, K>,
11}
12
13impl<A, K> Router<A, K> {
14    /// Create a new `Router` from the given input stream and selector
15    /// function.
16    pub fn new(
17        sodium_ctx: &SodiumCtx,
18        in_stream: &Stream<A>,
19        selector: impl Fn(&A) -> Vec<K> + Send + Sync + 'static,
20    ) -> Router<A, K>
21    where
22        A: Clone + Send + 'static,
23        K: Send + Sync + Eq + Hash + 'static,
24    {
25        Router {
26            impl_: RouterImpl::new(&sodium_ctx.impl_, &in_stream.impl_, selector),
27        }
28    }
29
30    /// Create a Stream that is subscribed to event values that the
31    /// selector function routes to the given `K` value.
32    pub fn filter_matches(&self, k: &K) -> Stream<A>
33    where
34        A: Clone + Send + 'static,
35        K: Clone + Send + Sync + Eq + Hash + 'static,
36    {
37        Stream {
38            impl_: self.impl_.filter_matches(k),
39        }
40    }
41}