mm1_node/runtime/
actor_key.rs1use std::fmt;
2use std::sync::Arc;
3
4#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5pub(crate) struct ActorKey(Arc<Node>);
6
7impl ActorKey {
8 pub(crate) fn root() -> Self {
9 Self(Arc::new(Node::Root))
10 }
11
12 pub(crate) fn child(&self, func: &'static str) -> Self {
13 let func = func.trim_matches('(');
14 let func = func.find('<').map(|at| &func[..at]).unwrap_or_else(|| func);
15 let node = Node::Child {
16 parent: self.0.clone(),
17 func,
18 };
19 Self(Arc::new(node))
20 }
21
22 pub(crate) fn path(&self) -> impl Iterator<Item = &str> + '_ {
23 let mut acc = vec![];
24 let mut node = self.0.as_ref();
25 loop {
26 match node {
27 Node::Root => break acc.into_iter().rev(),
28 Node::Child { parent, func } => {
29 acc.push(func);
30 node = parent.as_ref();
31 },
32 }
33 }
34 }
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
38enum Node {
39 Root,
40 Child {
41 parent: Arc<Self>,
42 func: &'static str,
43 },
44}
45
46impl fmt::Display for ActorKey {
47 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48 fmt::Display::fmt(self.0.as_ref(), f)
49 }
50}
51
52impl fmt::Display for Node {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 match self {
55 Self::Root => write!(f, ""),
56 Self::Child { parent, func } => {
57 fmt::Display::fmt(parent.as_ref(), f)?;
58 write!(f, "/")?;
59 write!(f, "{}", func)?;
60
61 Ok(())
62 },
63 }
64 }
65}