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
66
67
use core::any::TypeId;

use super::Protocol;

#[derive(Copy, Clone)]
pub struct ProtocolId {
    id: fn() -> TypeId,
    name: fn() -> &'static str,
}

impl ProtocolId {
    pub const fn of<P: Protocol>() -> Self {
        Self {
            id: || core::any::TypeId::of::<P>(),
            name: || core::any::type_name::<P>(),
        }
    }

    fn id(&self) -> TypeId {
        (self.id)()
    }

    fn name(&self) -> &'static str {
        (self.name)()
    }
}

impl core::fmt::Debug for ProtocolId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("ProtocolId")
            .field("id", &self.id())
            .field("name", &self.name())
            .finish()
    }
}

impl core::fmt::Display for ProtocolId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.name().fmt(f)
    }
}

impl PartialEq for ProtocolId {
    fn eq(&self, other: &Self) -> bool {
        self.id() == other.id()
    }
}

impl Eq for ProtocolId {}

impl PartialOrd for ProtocolId {
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ProtocolId {
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.id().cmp(&other.id())
    }
}

impl core::hash::Hash for ProtocolId {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.id().hash(state);
    }
}