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::{DataspaceRegistry, DataspaceUpdate, Subscription};
12
13mod resources;
14pub use self::resources::{
15    AcquireError, ResourceKind, ResourceLease, ResourceRegistry, ResourceRegistryAPIHandler, ResourceRegistryState,
16    ResourceRegistryWorker, ResourceSpecification, ResourceStatus,
17};
18
19/// An identifier used to key values in a [`DataspaceRegistry`].
20///
21/// Identifiers come in two flavors:
22/// - **Named**: a string-based identifier, using [`MetaString`] for efficient storage.
23/// - **Numeric**: a simple numeric identifier.
24#[derive(Clone, Debug, PartialEq, Eq, Hash)]
25pub enum Identifier {
26    /// A string-based identifier.
27    Named(MetaString),
28
29    /// A numeric identifier.
30    Numeric(usize),
31}
32
33impl Identifier {
34    /// Creates a named identifier.
35    pub fn named(name: impl Into<MetaString>) -> Self {
36        Self::Named(name.into())
37    }
38
39    /// Creates a numeric identifier.
40    pub fn numeric(id: usize) -> Self {
41        Self::Numeric(id)
42    }
43}
44
45impl From<&str> for Identifier {
46    fn from(s: &str) -> Self {
47        Self::Named(MetaString::from(s))
48    }
49}
50
51impl From<String> for Identifier {
52    fn from(s: String) -> Self {
53        Self::Named(MetaString::from(s))
54    }
55}
56
57impl From<MetaString> for Identifier {
58    fn from(s: MetaString) -> Self {
59        Self::Named(s)
60    }
61}
62
63impl From<usize> for Identifier {
64    fn from(id: usize) -> Self {
65        Self::Numeric(id)
66    }
67}
68
69impl From<Id> for Identifier {
70    fn from(id: Id) -> Self {
71        Self::Numeric(id.as_usize())
72    }
73}
74
75/// A filter used to match identifiers when subscribing to a [`DataspaceRegistry`].
76///
77/// Filters control which assertions and retractions a subscription receives:
78/// - [`All`](IdentifierFilter::All): receives updates for every identifier.
79/// - [`Exact`](IdentifierFilter::Exact): receives updates only for a specific identifier.
80/// - [`Prefix`](IdentifierFilter::Prefix): receives updates for named identifiers that start with a given prefix.
81#[derive(Clone, Debug)]
82pub enum IdentifierFilter {
83    /// Matches all identifiers.
84    All,
85
86    /// Matches a single exact identifier.
87    Exact(Identifier),
88
89    /// Matches named identifiers that start with the given prefix.
90    ///
91    /// Never matches numeric identifiers.
92    Prefix(MetaString),
93}
94
95impl IdentifierFilter {
96    /// Creates a filter that matches all identifiers.
97    pub fn all() -> Self {
98        Self::All
99    }
100
101    /// Creates a filter that matches a single exact identifier.
102    pub fn exact(id: impl Into<Identifier>) -> Self {
103        Self::Exact(id.into())
104    }
105
106    /// Creates a filter that matches named identifiers with the given prefix.
107    pub fn prefix(prefix: impl Into<MetaString>) -> Self {
108        Self::Prefix(prefix.into())
109    }
110
111    /// Returns `true` if this filter matches the given identifier.
112    pub fn matches(&self, id: &Identifier) -> bool {
113        match self {
114            Self::All => true,
115            Self::Exact(expected) => id == expected,
116            Self::Prefix(prefix) => match id {
117                Identifier::Named(name) => name.as_ref().starts_with(prefix.as_ref()),
118                Identifier::Numeric(_) => false,
119            },
120        }
121    }
122}