Skip to main content

hydro_lang/compile/
embedded_runtime.rs

1//! Runtime helpers for the embedded deployment backend.
2//!
3//! This module is NOT gated on the `build` feature so that the staged
4//! re-exports are available at runtime in the generated code.
5
6use futures::Stream;
7use stageleft::{QuotedWithContext, RuntimeData, q};
8
9use crate::location::MembershipEvent;
10use crate::location::member_id::TaglessMemberId;
11
12#[cfg_attr(
13    not(any(
14        feature = "deploy_integration",
15        feature = "docker_runtime",
16        feature = "maelstrom_runtime"
17    )),
18    expect(
19        unreachable_code,
20        reason = "uninhabited but deploy_integration required at embedded runtime"
21    )
22)]
23/// Returns a [`QuotedWithContext`] that references the `__cluster_self_id` runtime variable.
24pub fn embedded_cluster_self_id<'a>() -> impl QuotedWithContext<'a, TaglessMemberId, ()> + Clone + 'a
25{
26    let self_id: RuntimeData<&TaglessMemberId> = RuntimeData::new("__cluster_self_id");
27    q!(self_id.clone())
28}
29
30/// Returns a [`QuotedWithContext`] that references a `__membership_{idx}` runtime variable.
31pub fn embedded_cluster_membership_stream<'a>(
32    idx: usize,
33) -> impl QuotedWithContext<'a, Box<dyn Stream<Item = (TaglessMemberId, MembershipEvent)> + Unpin>, ()>
34{
35    // TODO(shadaj): change `Deploy` trait to use `syn::Expr` to avoid leaking here
36    // TODO(shadaj): this will not work if the same location reads the same membership stream multiple times
37    let var_name: &'static str = Box::leak(format!("__membership_{}", idx).into_boxed_str());
38    let membership: RuntimeData<
39        Box<dyn Stream<Item = (TaglessMemberId, MembershipEvent)> + Unpin>,
40    > = RuntimeData::new(var_name);
41    q!(membership)
42}