primitives/types/identifiers/
protocol_info.rs

1/// Static protocol information, used for logging and tagging messages for domain separation.
2///
3/// To ensure uniqueness, the `tag` should be a unique integer for each protocol.
4/// The `name` is a human-readable identifier for the protocol, useful for logging and debugging.
5///
6/// Use the `macros::new_protocol!` macro to create instances of this struct, which ensures
7/// that protocol tags are unique at compile time.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct ProtocolInfo {
10    /// A unique tag for the protocol, used for domain separation in signatures and hashes.
11    tag: [u8; 2],
12    /// The name of the protocol, used for logging and debugging.
13    name: &'static str,
14}
15
16impl ProtocolInfo {
17    /// Create a new `ProtocolInfo` instance. Warning! This does not check for tag uniqueness.
18    /// It is recommended to use the `macros::new_protocol!` macro instead.
19    pub const fn new(name: &'static str, tag: u16) -> Self {
20        let tag = tag.to_le_bytes();
21        Self { name, tag }
22    }
23
24    /// Get the name of the protocol.
25    pub const fn name(&self) -> &'static str {
26        self.name
27    }
28
29    /// Get the unique tag of the protocol.
30    pub const fn tag(&self) -> &[u8; 2] {
31        &self.tag
32    }
33}