qualia_core_db/bundle/stream/policy/grant/
scoped.rs1use super::{
3 AuthorityBinding, AuthorityOwner, BoundClosure, E, LocalWholeSourceGrant, MutationHandle,
4 PinAuthority,
5};
6
7pub struct LocalEffectControl<'a, 'data> {
11 owner: &'a mut AuthorityOwner,
12 closure: &'a BoundClosure<'data>,
13 pins: &'a PinAuthority,
14 clock: &'a dyn Fn() -> u64,
15 handle: MutationHandle,
16 binding: AuthorityBinding,
17 ceiling: u64,
18 terminal: bool,
19}
20impl LocalEffectControl<'_, '_> {
21 pub fn recheck(&mut self) -> Result<(), E> {
25 if self.terminal {
26 return Err(E::Spent);
27 }
28 self.terminal = true;
29 for _ in 0..2 {
30 let now = (self.clock)();
31 self.closure.recheck(self.pins, now)?;
32 self.owner
33 .issue_permit(self.handle, self.binding, now, self.ceiling)
34 .map_err(E::Authority)?;
35 }
36 self.terminal = false;
37 Ok(())
38 }
39 pub fn cancel(&mut self) -> Result<(), E> {
40 self.terminal = true;
41 self.owner.revoke(self.handle).map_err(E::Authority)
42 }
43 pub fn closure(&self) -> &BoundClosure<'_> {
44 self.closure
45 }
46}
47impl<'data> LocalWholeSourceGrant<'_, 'data> {
48 pub fn accept_local_scoped<T>(
56 &mut self,
57 pins: &PinAuthority,
58 clock: impl Fn() -> u64,
59 effect: impl for<'g> FnOnce(&mut LocalEffectControl<'g, 'data>) -> Result<T, E>,
60 ) -> Result<T, E> {
61 if self.spent {
62 return Err(E::Spent);
63 }
64 self.spent = true;
65 let mut control = LocalEffectControl {
66 owner: self.owner,
67 closure: self.closure,
68 pins,
69 clock: &clock,
70 handle: self.handle,
71 binding: self.binding,
72 ceiling: self.ceiling,
73 terminal: false,
74 };
75 control.recheck()?;
76 let result = effect(&mut control)?;
77 control.recheck()?;
78 Ok(result)
79 }
80}