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/// Returns a [`QuotedWithContext`] that references the `__cluster_self_id` runtime variable.
13pub fn embedded_cluster_self_id<'a>() -> impl QuotedWithContext<'a, TaglessMemberId, ()> + Clone + 'a
14{
15    let self_id: RuntimeData<&TaglessMemberId> = RuntimeData::new("__cluster_self_id");
16    q!(self_id.clone())
17}
18
19/// Returns a [`QuotedWithContext`] that references a `__membership_{idx}` runtime variable.
20pub fn embedded_cluster_membership_stream<'a>(
21    idx: usize,
22) -> impl QuotedWithContext<'a, Box<dyn Stream<Item = (TaglessMemberId, MembershipEvent)> + Unpin>, ()>
23{
24    // TODO(shadaj): change `Deploy` trait to use `syn::Expr` to avoid leaking here
25    // TODO(shadaj): this will not work if the same location reads the same membership stream multiple times
26    let var_name: &'static str = Box::leak(format!("__membership_{}", idx).into_boxed_str());
27    let membership: RuntimeData<
28        Box<dyn Stream<Item = (TaglessMemberId, MembershipEvent)> + Unpin>,
29    > = RuntimeData::new(var_name);
30    q!(membership)
31}