Skip to main content

hydro_lang/location/
member_id.rs

1//! Typed identifiers for members of a [`Cluster`](super::Cluster).
2//!
3//! In Hydro, a [`Cluster`](super::Cluster) is a location that represents a group of
4//! identical processes. Each process in a cluster is identified by a [`MemberId`].
5//!
6//! A `MemberId<Tag, Id>` has two type parameters.
7//! `Tag` names the member's cluster, so IDs from different clusters cannot be mixed up.
8//! `Id` is the type of the ID itself, such as an index, an address, or a name.
9//!
10//! You choose `Id` when you create the cluster (see
11//! [`FlowBuilder::cluster`](crate::compile::builder::FlowBuilder::cluster)).
12//! Pick whatever the code that runs the cluster already uses to identify its members.
13//! Hydro only compares, hashes, orders, clones, and sends IDs, so it works with any such type.
14//! The deployment backend must support it (see
15//! [`SupportsMemberId`](crate::compile::deploy_provider::SupportsMemberId)).
16
17use std::fmt::{Debug, Display};
18use std::hash::Hash;
19use std::marker::PhantomData;
20
21use serde::{Deserialize, Serialize};
22
23/// A typed identifier for a member of a [`Cluster`](super::Cluster).
24///
25/// The `Tag` type parameter ties this ID to a specific cluster, preventing
26/// accidental mixing of member IDs from different clusters at compile time.
27/// It wraps an `Id` (see the [module documentation](self)).
28#[repr(transparent)]
29pub struct MemberId<Tag, Id = u32> {
30    inner: Id,
31    _phantom: PhantomData<Tag>,
32}
33
34impl<Tag, Id> MemberId<Tag, Id> {
35    /// Unwraps the ID, dropping the cluster tag.
36    pub fn into_tagless(self) -> Id {
37        self.inner
38    }
39
40    /// Wraps an ID as a member of this cluster.
41    pub fn from_tagless(inner: Id) -> Self {
42        Self {
43            inner,
44            _phantom: PhantomData,
45        }
46    }
47
48    /// The wrapped ID.
49    pub fn as_tagless(&self) -> &Id {
50        &self.inner
51    }
52}
53
54impl<Tag> MemberId<Tag, u32> {
55    /// The member numbered `raw_id`.
56    ///
57    /// Hydro Deploy and the simulator number each cluster's members `0..n`.
58    pub fn from_raw_id(raw_id: u32) -> Self {
59        Self::from_tagless(raw_id)
60    }
61
62    /// This member's number.
63    pub fn get_raw_id(&self) -> u32 {
64        self.inner
65    }
66}
67
68impl<Tag, Id: Debug> Debug for MemberId<Tag, Id> {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        write!(
71            f,
72            "MemberId::<{}>({:?})",
73            std::any::type_name::<Tag>(),
74            self.inner
75        )
76    }
77}
78
79impl<Tag, Id: Display> Display for MemberId<Tag, Id> {
80    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81        write!(
82            f,
83            "MemberId::<{}>({})",
84            std::any::type_name::<Tag>(),
85            self.inner
86        )
87    }
88}
89
90impl<Tag, Id: Clone> Clone for MemberId<Tag, Id> {
91    fn clone(&self) -> Self {
92        Self::from_tagless(self.inner.clone())
93    }
94}
95
96impl<Tag, Id: Serialize> Serialize for MemberId<Tag, Id> {
97    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
98    where
99        S: serde::Serializer,
100    {
101        self.inner.serialize(serializer)
102    }
103}
104
105impl<'a, Tag, Id: Deserialize<'a>> Deserialize<'a> for MemberId<Tag, Id> {
106    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
107    where
108        D: serde::Deserializer<'a>,
109    {
110        Ok(Self::from_tagless(Id::deserialize(deserializer)?))
111    }
112}
113
114impl<Tag, Id: Ord> PartialOrd for MemberId<Tag, Id> {
115    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
116        Some(self.cmp(other))
117    }
118}
119
120impl<Tag, Id: Ord> Ord for MemberId<Tag, Id> {
121    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
122        self.inner.cmp(&other.inner)
123    }
124}
125
126impl<Tag, Id: PartialEq> PartialEq for MemberId<Tag, Id> {
127    fn eq(&self, other: &Self) -> bool {
128        self.inner == other.inner
129    }
130}
131
132impl<Tag, Id: Eq> Eq for MemberId<Tag, Id> {}
133
134impl<Tag, Id: Hash> Hash for MemberId<Tag, Id> {
135    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
136        self.inner.hash(state);
137        // This seems like the a good thing to do. This will ensure that two member ids that come from different
138        // clusters but the same underlying host receive different hashes.
139        std::any::type_name::<Tag>().hash(state);
140    }
141}