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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use core::any::TypeId;

use super::Protocol;

/// ID of a protocol.
///
/// An ID also includes the protocol's type name for easier debugging.
#[derive(Copy, Clone)]
pub struct ProtocolId {
    /// Type ID of the protocol type.
    id: fn() -> TypeId,

    /// Name of the protocol type.
    name: fn() -> &'static str,
}

impl ProtocolId {
    /// Get the ID of a protocol.
    pub const fn of<P: Protocol>() -> Self {
        Self {
            id: || core::any::TypeId::of::<P>(),
            name: || core::any::type_name::<P>(),
        }
    }

    /// Type ID of the protocol.
    ///
    /// This is used for comparision.
    fn id(&self) -> TypeId {
        (self.id)()
    }

    /// Type name of the protocol.
    ///
    /// This is used for debugging purposes.
    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 {
        // Just print the type name.
        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);
    }
}