somatize_core/host.rs
1//! Which process a node runs in.
2//!
3//! A **name**, not an address, and that is the whole decision. A
4//! [`Placement`](crate::Placement) is data: if it carried
5//! `tcp://10.0.0.2:7000` inside, the same graph could no longer run on another
6//! cluster without editing it. With a name, whoever **executes** decides what
7//! `worker1` resolves to — the same boundary a `Transport` draws.
8//!
9//! And that is why it is not an enum, even though [`Device`](crate::Device) is.
10//! A device is a closed set we decide, and a typo has to fail at declaration
11//! time. Hosts are named by the user and there is no list to close.
12
13use std::fmt;
14
15/// The name of the process where a node runs.
16#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
17#[cfg_attr(
18 feature = "serde",
19 derive(serde::Serialize, serde::Deserialize),
20 serde(transparent)
21)]
22pub struct Host(String);
23
24impl Host {
25 /// A host by its name.
26 pub fn new(name: impl Into<String>) -> Self {
27 Self(name.into())
28 }
29
30 /// The name.
31 pub fn as_str(&self) -> &str {
32 &self.0
33 }
34}
35
36impl From<&str> for Host {
37 fn from(name: &str) -> Self {
38 Self(name.to_string())
39 }
40}
41
42impl From<String> for Host {
43 fn from(name: String) -> Self {
44 Self(name)
45 }
46}
47
48impl fmt::Display for Host {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 f.write_str(&self.0)
51 }
52}