mm1_node/runtime/
actor_key.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::fmt;
use std::sync::Arc;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct ActorKey(Arc<Node>);

impl ActorKey {
    pub(crate) fn root() -> Self {
        Self(Arc::new(Node::Root))
    }

    pub(crate) fn child(&self, func: &'static str) -> Self {
        let func = func.trim_matches('(');
        let func = func.find('<').map(|at| &func[..at]).unwrap_or_else(|| func);
        let node = Node::Child {
            parent: self.0.clone(),
            func,
        };
        Self(Arc::new(node))
    }

    pub(crate) fn path(&self) -> impl Iterator<Item = &str> + '_ {
        let mut acc = vec![];
        let mut node = self.0.as_ref();
        loop {
            match node {
                Node::Root => break acc.into_iter().rev(),
                Node::Child { parent, func } => {
                    acc.push(func);
                    node = parent.as_ref();
                },
            }
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum Node {
    Root,
    Child {
        parent: Arc<Self>,
        func:   &'static str,
    },
}

impl fmt::Display for ActorKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self.0.as_ref(), f)
    }
}

impl fmt::Display for Node {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Root => write!(f, ""),
            Self::Child { parent, func } => {
                fmt::Display::fmt(parent.as_ref(), f)?;
                write!(f, "/")?;
                write!(f, "{}", func)?;

                Ok(())
            },
        }
    }
}