Skip to main content

bevy_networker_multiplayer/
netmsg.rs

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