Thing 2.0: the shared interest Ledger, the bind diagnostic codes, is_stored_at_stamp from the contract
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
491fc2020f
commit
b640b3c927
5 changed files with 65 additions and 2 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
53
crates/thing-core/src/port/ledger.rs
Normal file
53
crates/thing-core/src/port/ledger.rs
Normal file
|
|
@ -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<Interest, (SubscriptionId, usize)>,
|
||||
}
|
||||
|
||||
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<Interest>, new: &HashSet<Interest>) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
@ -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};
|
||||
|
|
|
|||
|
|
@ -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<R>(step: impl FnOnce() -> R) -> R {
|
||||
pub(crate) fn grow<R>(step: impl FnOnce() -> R) -> R {
|
||||
stacker::maybe_grow(RED_ZONE, SEGMENT, step)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue