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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::impl_::sodium_ctx::SodiumCtx as SodiumCtxImpl;
use crate::Cell;
use crate::CellLoop;
use crate::CellSink;
use crate::Router;
use crate::Stream;
use crate::StreamLoop;
use crate::StreamSink;
use crate::Transaction;
use std::hash::Hash;

#[derive(Clone)]
pub struct SodiumCtx {
    pub impl_: SodiumCtxImpl,
}

impl Default for SodiumCtx {
    fn default() -> SodiumCtx {
        SodiumCtx::new()
    }
}

impl SodiumCtx {
    pub fn new() -> SodiumCtx {
        SodiumCtx {
            impl_: SodiumCtxImpl::new(),
        }
    }

    pub fn new_cell<A: Clone + Send + 'static>(&self, a: A) -> Cell<A> {
        Cell::new(self, a)
    }

    pub fn new_stream<A: Clone + Send + 'static>(&self) -> Stream<A> {
        Stream::new(self)
    }

    pub fn new_cell_sink<A: Clone + Send + 'static>(&self, a: A) -> CellSink<A> {
        CellSink::new(self, a)
    }

    pub fn new_stream_sink<A: Clone + Send + 'static>(&self) -> StreamSink<A> {
        StreamSink::new(self)
    }

    pub fn new_cell_loop<A: Clone + Send + 'static>(&self) -> CellLoop<A> {
        CellLoop::new(self)
    }

    pub fn new_stream_loop<A: Clone + Send + 'static>(&self) -> StreamLoop<A> {
        StreamLoop::new(self)
    }

    pub fn new_stream_sink_with_coalescer<
        A: Clone + Send + 'static,
        COALESCER: FnMut(&A, &A) -> A + Send + 'static,
    >(
        &self,
        coalescer: COALESCER,
    ) -> StreamSink<A> {
        StreamSink::new_with_coalescer(self, coalescer)
    }

    pub fn transaction<R, K: FnOnce() -> R>(&self, k: K) -> R {
        self.impl_.transaction(k)
    }

    pub fn new_transaction(&self) -> Transaction {
        Transaction::new(self)
    }

    pub fn post<K: FnMut() + Send + 'static>(&self, k: K) {
        self.impl_.post(k);
    }

    pub fn new_router<A, K>(
        &self,
        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::new(self, in_stream, selector)
    }
}