Diffstat (limited to 'src/protocol/id.rs')
-rw-r--r--src/protocol/id.rs67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/protocol/id.rs b/src/protocol/id.rs
new file mode 100644
index 0000000..f1a1723
--- /dev/null
+++ b/src/protocol/id.rs
@@ -0,0 +1,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);
+ }
+}