harmos_runtime/state/
managed.rs1use std::sync::Arc;
18
19use super::{At, Error, Origin, Position, TrackedState};
23use crate::Application;
24
25pub struct ManagedState<A: Application> {
34 tracked: Arc<TrackedState<A>>,
35}
36
37impl<A: Application> ManagedState<A> {
44 pub(super) fn new(tracked: Arc<TrackedState<A>>) -> Self {
45 Self { tracked }
46 }
47
48 pub async fn read<R>(&self, read: impl FnOnce(&A::State) -> R) -> Result<At<R>, Error> {
49 if self.tracked.poisoned() {
50 return Err(self.tracked.unavailable());
51 }
52 let state = self.tracked.state.read().await;
53 if self.tracked.poisoned() {
55 return Err(self.tracked.unavailable());
56 }
57
58 Ok(At { value: read(&state.value), position: state.position })
59 }
60
61 pub async fn at(&self, origin: Origin<A::State>, at: Position) -> Result<At<A::State>, Error> {
62 if self.tracked.poisoned() {
63 return Err(self.tracked.unavailable());
64 }
65
66 {
67 let tail = self.tracked.tail.read().await;
68 if tail.splits_commit(at) || tail.splits_commit(origin.position) {
69 return Err(Error::IncompleteCommit);
70 }
71 }
72 self.tracked.reads.state(origin, at).await
73 }
74}
75
76impl<A: Application> Clone for ManagedState<A> {
77 fn clone(&self) -> Self {
78 Self::new(Arc::clone(&self.tracked))
79 }
80}
81
82