sodium_rust/
sodium_ctx.rs1use crate::impl_::sodium_ctx::SodiumCtx as SodiumCtxImpl;
2use crate::Cell;
3use crate::CellLoop;
4use crate::CellSink;
5use crate::Router;
6use crate::Stream;
7use crate::StreamLoop;
8use crate::StreamSink;
9use crate::Transaction;
10use std::hash::Hash;
11
12#[derive(Clone)]
15pub struct SodiumCtx {
16 pub impl_: SodiumCtxImpl,
17}
18
19impl Default for SodiumCtx {
20 fn default() -> SodiumCtx {
21 SodiumCtx::new()
22 }
23}
24
25impl SodiumCtx {
26 pub fn new() -> SodiumCtx {
28 SodiumCtx {
29 impl_: SodiumCtxImpl::new(),
30 }
31 }
32
33 pub fn new_cell<A: Clone + Send + 'static>(&self, a: A) -> Cell<A> {
35 Cell::new(self, a)
36 }
37
38 pub fn new_stream<A: Clone + Send + 'static>(&self) -> Stream<A> {
40 Stream::new(self)
41 }
42
43 pub fn new_cell_sink<A: Clone + Send + 'static>(&self, a: A) -> CellSink<A> {
45 CellSink::new(self, a)
46 }
47
48 pub fn new_stream_sink<A: Clone + Send + 'static>(&self) -> StreamSink<A> {
50 StreamSink::new(self)
51 }
52
53 pub fn new_cell_loop<A: Clone + Send + 'static>(&self) -> CellLoop<A> {
56 CellLoop::new(self)
57 }
58
59 pub fn new_stream_loop<A: Clone + Send + 'static>(&self) -> StreamLoop<A> {
62 StreamLoop::new(self)
63 }
64
65 pub fn new_stream_sink_with_coalescer<
69 A: Clone + Send + 'static,
70 COALESCER: FnMut(&A, &A) -> A + Send + 'static,
71 >(
72 &self,
73 coalescer: COALESCER,
74 ) -> StreamSink<A> {
75 StreamSink::new_with_coalescer(self, coalescer)
76 }
77
78 pub fn transaction<R, K: FnOnce() -> R>(&self, k: K) -> R {
81 self.impl_.transaction(k)
82 }
83
84 pub fn new_transaction(&self) -> Transaction {
90 Transaction::new(self)
91 }
92
93 pub fn post<K: FnMut() + Send + 'static>(&self, k: K) {
96 self.impl_.post(k);
97 }
98
99 pub fn new_router<A, K>(
101 &self,
102 in_stream: &Stream<A>,
103 selector: impl Fn(&A) -> Vec<K> + Send + Sync + 'static,
104 ) -> Router<A, K>
105 where
106 A: Clone + Send + 'static,
107 K: Send + Sync + Eq + Hash + 'static,
108 {
109 Router::new(self, in_stream, selector)
110 }
111}