sodium_rust/cell_sink.rs
1use crate::cell::Cell;
2use crate::impl_::cell_sink::CellSink as CellSinkImpl;
3use crate::sodium_ctx::SodiumCtx;
4
5/// A [`Cell`] that allows values to be pushed into it, acting as a
6/// interface between the world of I/O and the world of FRP.
7///
8/// ## Note: This should only be used from _outside_ the context of
9/// the Sodium system to inject data from I/O into the reactive system.
10pub struct CellSink<A> {
11 pub impl_: CellSinkImpl<A>,
12}
13
14impl<A> Clone for CellSink<A> {
15 fn clone(&self) -> Self {
16 CellSink {
17 impl_: self.impl_.clone(),
18 }
19 }
20}
21
22impl<A: Clone + Send + 'static> CellSink<A> {
23 /// Create a new `CellSink` in the given context.
24 pub fn new(sodium_ctx: &SodiumCtx, a: A) -> CellSink<A> {
25 CellSink {
26 impl_: CellSinkImpl::new(&sodium_ctx.impl_, a),
27 }
28 }
29
30 /// Return a [`Cell`] that can be used to create Sodium logic that
31 /// will read the values pushed into this `CellSink` from the I/O
32 /// world.
33 pub fn cell(&self) -> Cell<A> {
34 Cell {
35 impl_: self.impl_.cell(),
36 }
37 }
38
39 /// Send a value, modifying the value of the cell.
40 ///
41 /// This method may not be called in handlers registered with
42 /// [`Stream::listen`][crate::Stream::listen] or [`Cell::listen`].
43 ///
44 /// `CellSink` is an operational primitive, meant for interfacing
45 /// I/O to FRP only. You aren't meant to use this to define your
46 /// own primitives.
47 pub fn send(&self, a: A) {
48 self.impl_.send(a);
49 }
50}