diff --git a/crates/thing-contract/src/lib.rs b/crates/thing-contract/src/lib.rs index 602e81a..62085b9 100644 --- a/crates/thing-contract/src/lib.rs +++ b/crates/thing-contract/src/lib.rs @@ -33,7 +33,7 @@ pub use result::*; pub use source::*; pub use style::*; -pub use cultcache_rs::DatabaseEntry; +pub use cultcache_rs::{DatabaseEntry, is_stored_at_stamp}; use cultcache_rs::{CultCache, CultCacheRegistry}; /// Registers the five kinds with a cache. diff --git a/crates/thing-core/src/diagnostic.rs b/crates/thing-core/src/diagnostic.rs index 7d8f174..e5a69fb 100644 --- a/crates/thing-core/src/diagnostic.rs +++ b/crates/thing-core/src/diagnostic.rs @@ -25,6 +25,14 @@ pub enum Code { /// The input nests deeper than the expander can recurse. A stack bound, not the depth rule: the depth /// rule is judged on the finished tree and reports ExpansionLimit. TooDeepToResolve, + /// A state record held under another schema than the Bind names. + BindSchemaMismatch, + /// A member path names a member the value does not have. + BindMemberMissing, + /// A value of another type than its site takes; no value is coerced. + BindTypeMismatch, + /// Two items of one Repeat share a key. + RepeatKeyDuplicate, } impl Code { diff --git a/crates/thing-core/src/port/ledger.rs b/crates/thing-core/src/port/ledger.rs new file mode 100644 index 0000000..57a7a27 --- /dev/null +++ b/crates/thing-core/src/port/ledger.rs @@ -0,0 +1,53 @@ +//! The one interest ledger: core holds a state interest once however many readers ask for it. A +//! reader (the Resolver or the Binder) holds and releases through the ledger, never through the +//! port, so one `SubscriptionId` per interest is shared by count and an interest nothing reads +//! is unsubscribed. + +use std::collections::{HashMap, HashSet}; + +use super::{Interest, StatePort, SubscriptionId}; + +#[derive(Debug, Default)] +pub struct Ledger { + held: HashMap, +} + +impl Ledger { + pub fn new() -> Self { + Self::default() + } + + /// One more reader of `interest`; the port is asked only for the first. + pub fn hold(&mut self, port: &mut dyn StatePort, interest: &Interest) -> SubscriptionId { + let (id, count) = self.held.entry(interest.clone()).or_insert_with(|| (port.subscribe(interest.clone()), 0)); + *count += 1; + *id + } + + /// One reader fewer; the port is told when the last goes. Releasing what is not held does nothing. + pub fn release(&mut self, port: &mut dyn StatePort, interest: &Interest) { + let Some((id, count)) = self.held.get_mut(interest) else { return }; + *count -= 1; + if *count == 0 { + let id = *id; + self.held.remove(interest); + port.unsubscribe(id); + } + } + + /// Move one reader from the interests it held to the ones it holds now: hold only the added, + /// release only the removed, touch nothing else. + pub fn diff(&mut self, port: &mut dyn StatePort, old: &HashSet, new: &HashSet) { + for added in new.difference(old) { + self.hold(port, added); + } + for removed in old.difference(new) { + self.release(port, removed); + } + } + + /// How many readers hold `interest`. + pub fn readers(&self, interest: &Interest) -> usize { + self.held.get(interest).map_or(0, |(_, count)| *count) + } +} diff --git a/crates/thing-core/src/port/mod.rs b/crates/thing-core/src/port/mod.rs index 1ae2ae8..6c9962c 100644 --- a/crates/thing-core/src/port/mod.rs +++ b/crates/thing-core/src/port/mod.rs @@ -1,8 +1,10 @@ //! What core may ask of state. The port reads and queues changes; nothing in it writes, and //! it never awaits: core drains queued changes inside its frame. +pub mod ledger; pub mod state; +pub use ledger::Ledger; pub use state::StateValue; use thing_contract::{AssetCatalog, CommandResult, CompositionDocument, ProviderId, RecordKey, SchemaId, SurfaceDocument}; diff --git a/crates/thing-core/src/resolve/mod.rs b/crates/thing-core/src/resolve/mod.rs index 002eb9f..921d85c 100644 --- a/crates/thing-core/src/resolve/mod.rs +++ b/crates/thing-core/src/resolve/mod.rs @@ -19,7 +19,7 @@ const RED_ZONE: usize = 512 * 1024; const SEGMENT: usize = 2 * 1024 * 1024; /// Runs one recursive step with room for the next, growing the stack when it is nearly spent. -fn grow(step: impl FnOnce() -> R) -> R { +pub(crate) fn grow(step: impl FnOnce() -> R) -> R { stacker::maybe_grow(RED_ZONE, SEGMENT, step) }