sodium_rust/
cell_loop.rs

1use crate::impl_::cell_loop::CellLoop as CellLoopImpl;
2use crate::Cell;
3use crate::SodiumCtx;
4
5/// A forward reference for a [`Cell`] for creating dependency loops.
6///
7/// Both the creation of a `CellLoop` and filling it with the
8/// referenced [`Cell`] by calling [`loop_`][CellLoop::loop_] _must_
9/// occur within the same transaction, whether that is created by
10/// calling [`SodiumCtx::transaction`] or
11/// [`Transaction::new`][crate::Transaction::new].
12pub struct CellLoop<A> {
13    impl_: CellLoopImpl<A>,
14}
15
16impl<A> Clone for CellLoop<A> {
17    fn clone(&self) -> Self {
18        CellLoop {
19            impl_: self.impl_.clone(),
20        }
21    }
22}
23
24impl<A: Send + Clone + 'static> CellLoop<A> {
25    /// Create a new `CellLoop` in the given context.
26    pub fn new(sodium_ctx: &SodiumCtx) -> CellLoop<A> {
27        CellLoop {
28            impl_: CellLoopImpl::new(&sodium_ctx.impl_),
29        }
30    }
31
32    /// Return a [`Cell`] that is equivalent to this `CellLoop` once it
33    /// has been resolved by calling [`loop_`][CellLoop::loop_].
34    ///
35    /// If a [`Cell`] created by `CellLoop` may be passed to an
36    /// abstraction that uses [`Cell::sample`], then
37    /// [`Cell::sample_lazy`] should be used instead.
38    pub fn cell(&self) -> Cell<A> {
39        Cell {
40            impl_: self.impl_.cell(),
41        }
42    }
43
44    /// Resolve the loop to specify what this `CellLoop` was a forward
45    /// reference to.
46    ///
47    /// This function must be invoked in the same transaction as the
48    /// place where the `CellLoop` was created. This requires creating
49    /// an explicit transaction, either with
50    /// [`SodiumCtx::transaction`] or
51    /// [`Transaction::new`][crate::Transaction::new].
52    pub fn loop_(&self, ca: &Cell<A>) {
53        self.impl_.loop_(&ca.impl_);
54    }
55}