sodium_rust/
sodium_ctx.rs

1use 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/// A context object representing a specific instance of a Sodium
13/// system.
14#[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    /// Create a new Sodium FRP context.
27    pub fn new() -> SodiumCtx {
28        SodiumCtx {
29            impl_: SodiumCtxImpl::new(),
30        }
31    }
32
33    /// Create a new constant value [`Cell`] in this context.
34    pub fn new_cell<A: Clone + Send + 'static>(&self, a: A) -> Cell<A> {
35        Cell::new(self, a)
36    }
37
38    /// Create a new stream that will never fire in this context.
39    pub fn new_stream<A: Clone + Send + 'static>(&self) -> Stream<A> {
40        Stream::new(self)
41    }
42
43    /// Create a new [`CellSink`] for interfacing I/O and FRP.
44    pub fn new_cell_sink<A: Clone + Send + 'static>(&self, a: A) -> CellSink<A> {
45        CellSink::new(self, a)
46    }
47
48    /// Create a new [`StreamSink`] for interfacing I/O and FRP.
49    pub fn new_stream_sink<A: Clone + Send + 'static>(&self) -> StreamSink<A> {
50        StreamSink::new(self)
51    }
52
53    /// Create a new [`CellLoop`] to act as a forward reference for a
54    /// [`Cell`] that will be created later.
55    pub fn new_cell_loop<A: Clone + Send + 'static>(&self) -> CellLoop<A> {
56        CellLoop::new(self)
57    }
58
59    /// Create a new [`StreamLoop`] to act as a forward reference for
60    /// a [`Stream`] that will be created later.
61    pub fn new_stream_loop<A: Clone + Send + 'static>(&self) -> StreamLoop<A> {
62        StreamLoop::new(self)
63    }
64
65    /// Create a new [`StreamSink`] with a combining function that
66    /// allows [`send`][CellSink::send]ing multiple event values per
67    /// transaction.
68    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    /// Run the given function inside a single Sodium transaction,
79    /// closing the transaction after the function returns.
80    pub fn transaction<R, K: FnOnce() -> R>(&self, k: K) -> R {
81        self.impl_.transaction(k)
82    }
83
84    /// Create a new scoped transaction object.
85    ///
86    /// The Sodium transaction on this context will be held open until
87    /// the returned [`Transaction`] is dropped or
88    /// [Transaction::close] is called explicitly.
89    pub fn new_transaction(&self) -> Transaction {
90        Transaction::new(self)
91    }
92
93    /// Execute the given code after the current transaction is
94    /// closed, or immediately if there is no current transaction.
95    pub fn post<K: FnMut() + Send + 'static>(&self, k: K) {
96        self.impl_.post(k);
97    }
98
99    /// Create a new [`Router`] in this context.
100    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}