motorcortex_rust/msg/hash.rs
1/// Represents a trait for associating a unique, compile-time constant hash with a type.
2pub trait Hash {
3 const HASH: u32;
4}
5
6/// Retrieve a compile-time constant hash for a message type.
7///
8/// This hash uniquely identifies the type and can be used for version tracking,
9/// serialization metadata, or message validation in distributed environments.
10///
11/// Usage:
12/// ```
13/// use motorcortex_rust::get_hash;
14/// use motorcortex_rust::SessionTokenMsg;
15///
16/// fn main() {
17/// let hash = get_hash::<SessionTokenMsg>();
18/// println!("Hash for SessionTokenMsg: {hash:#x}");
19/// }
20/// ```
21pub fn get_hash<T: Hash>() -> u32 {
22 T::HASH
23}
24
25/// Retrieve the size, in bytes, of a compile-time constant hash used in the system.
26///
27/// # Returns
28/// - A `u32` value representing the size of the constant hash (currently always 4).
29///
30/// # Usage
31/// ```
32/// use motorcortex_rust::get_hash_size;
33///
34/// fn main() {
35/// let size = get_hash_size();
36/// println!("Hash size: {size} bytes");
37/// }
38/// ```
39pub fn get_hash_size() -> usize {
40 size_of::<u32>()
41}