Skip to main content

saluki_core/runtime/state/
mod.rs

1//! Runtime state management utilities.
2//!
3//! This module provides utilities for managing shared state across processes in the runtime system.
4
5use stringtheory::MetaString;
6
7use crate::runtime::process::Id;
8
9mod dataspace;
10pub(crate) use self::dataspace::CURRENT_DATASPACE;
11pub use self::dataspace::{AssertionUpdate, DataspaceRegistry, Subscription};
12
13/// An identifier used to key values in a [`DataspaceRegistry`].
14///
15/// Identifiers come in two flavors:
16/// - **Named**: a string-based identifier, using [`MetaString`] for efficient storage.
17/// - **Numeric**: a simple numeric identifier.
18#[derive(Clone, Debug, PartialEq, Eq, Hash)]
19pub enum Identifier {
20    /// A string-based identifier.
21    Named(MetaString),
22
23    /// A numeric identifier.
24    Numeric(usize),
25}
26
27impl Identifier {
28    /// Creates a named identifier.
29    pub fn named(name: impl Into<MetaString>) -> Self {
30        Self::Named(name.into())
31    }
32
33    /// Creates a numeric identifier.
34    pub fn numeric(id: usize) -> Self {
35        Self::Numeric(id)
36    }
37}
38
39impl From<&str> for Identifier {
40    fn from(s: &str) -> Self {
41        Self::Named(MetaString::from(s))
42    }
43}
44
45impl From<MetaString> for Identifier {
46    fn from(s: MetaString) -> Self {
47        Self::Named(s)
48    }
49}
50
51impl From<usize> for Identifier {
52    fn from(id: usize) -> Self {
53        Self::Numeric(id)
54    }
55}
56
57impl From<Id> for Identifier {
58    fn from(id: Id) -> Self {
59        Self::Numeric(id.as_usize())
60    }
61}
62
63/// A filter used to match identifiers when subscribing to a [`DataspaceRegistry`].
64///
65/// Filters control which assertions and retractions a subscription receives:
66/// - [`All`](IdentifierFilter::All): receives updates for every identifier.
67/// - [`Exact`](IdentifierFilter::Exact): receives updates only for a specific identifier.
68/// - [`Prefix`](IdentifierFilter::Prefix): receives updates for named identifiers that start with a given prefix.
69#[derive(Clone, Debug)]
70pub enum IdentifierFilter {
71    /// Matches all identifiers.
72    All,
73
74    /// Matches a single exact identifier.
75    Exact(Identifier),
76
77    /// Matches named identifiers that start with the given prefix.
78    ///
79    /// Never matches numeric identifiers.
80    Prefix(MetaString),
81}
82
83impl IdentifierFilter {
84    /// Creates a filter that matches all identifiers.
85    pub fn all() -> Self {
86        Self::All
87    }
88
89    /// Creates a filter that matches a single exact identifier.
90    pub fn exact(id: impl Into<Identifier>) -> Self {
91        Self::Exact(id.into())
92    }
93
94    /// Creates a filter that matches named identifiers with the given prefix.
95    pub fn prefix(prefix: impl Into<MetaString>) -> Self {
96        Self::Prefix(prefix.into())
97    }
98
99    /// Returns `true` if this filter matches the given identifier.
100    pub fn matches(&self, id: &Identifier) -> bool {
101        match self {
102            Self::All => true,
103            Self::Exact(expected) => id == expected,
104            Self::Prefix(prefix) => match id {
105                Identifier::Named(name) => name.as_ref().starts_with(prefix.as_ref()),
106                Identifier::Numeric(_) => false,
107            },
108        }
109    }
110}