Skip to main content

crux_core/bridge/
registry.rs

1use std::sync::Mutex;
2
3use facet::Facet;
4use serde::{Deserialize, Serialize};
5use slab::Slab;
6
7use super::{BridgeError, FfiFormat, Request};
8use crate::bridge::request_serde::ResolveSerialized;
9use crate::{EffectFFI, ResolveError};
10
11#[allow(clippy::unsafe_derive_deserialize)]
12#[derive(Facet, Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(transparent)]
14#[facet(transparent)]
15pub struct EffectId(pub u32);
16
17pub struct ResolveRegistry<T: FfiFormat>(Mutex<Slab<ResolveSerialized<T>>>);
18
19impl<T: FfiFormat> Default for ResolveRegistry<T> {
20    fn default() -> Self {
21        Self(Mutex::new(Slab::with_capacity(1024)))
22    }
23}
24
25impl<T: FfiFormat> ResolveRegistry<T> {
26    /// Register an effect for future continuation, when it has been processed
27    /// and output given back to the core.
28    ///
29    /// The `effect` will be serialized into its FFI counterpart before being stored
30    /// and wrapped in a [`Request`].
31    // used in docs/internals/bridge.md
32    // ANCHOR: register
33    pub fn register<Eff>(&self, effect: Eff) -> Request<Eff::Ffi>
34    where
35        Eff: EffectFFI,
36    {
37        let (effect, resolve) = effect.serialize();
38
39        let id = self
40            .0
41            .lock()
42            .expect("Registry Mutex poisoned.")
43            .insert(resolve);
44
45        Request {
46            id: EffectId(id.try_into().expect("EffectId overflow")),
47            effect,
48        }
49    }
50    // ANCHOR_END: register
51
52    /// Resume a previously registered effect. This may fail, either because `EffectId` wasn't
53    /// found or because this effect was not expected to be resumed again.
54    pub fn resume(&self, id: EffectId, response: &[u8]) -> Result<(), BridgeError<T>> {
55        let mut registry_lock = self.0.lock().expect("Registry Mutex poisoned");
56
57        let entry = registry_lock.get_mut(id.0 as usize);
58
59        let Some(entry) = entry else {
60            return Err(BridgeError::ProcessResponse(ResolveError::NotFound(id)));
61        };
62
63        let resolved = entry.resolve(response);
64
65        if let ResolveSerialized::Never = entry {
66            registry_lock.remove(id.0 as usize);
67        }
68
69        resolved
70    }
71}