Skip to main content

bevy_networker_multiplayer/
netmsg.rs

1// SPDX-License-Identifier: MIT
2use serde::{de::DeserializeOwned, Serialize};
3
4/// Trait implemented by typed network messages.
5///
6/// `#[netmsg]` generates this automatically for concrete message structs.
7pub trait NetMessage:
8    Serialize + DeserializeOwned + Clone + Send + Sync + 'static
9{
10    /// Fully qualified type path used to produce a stable wire identifier.
11    const TYPE_PATH: &'static str;
12    /// Stable wire identifier derived from `TYPE_PATH`.
13    const WIRE_ID: u64;
14}
15
16/// FNV-1a hash used to derive wire identifiers from type paths.
17pub const fn hash_type_path(type_path: &str) -> u64 {
18    let bytes = type_path.as_bytes();
19    let mut hash: u64 = 0xcbf29ce484222325;
20    let mut index = 0;
21    while index < bytes.len() {
22        hash ^= bytes[index] as u64;
23        hash = hash.wrapping_mul(0x100000001b3);
24        index += 1;
25    }
26    hash
27}