fidius-guest::hash
Rust
FNV-1a interface hashing for compile-time ABI drift detection.
The proc macro computes an interface_hash from the sorted required method
signatures of a trait. The host checks this hash at load time to reject
plugins compiled against a different interface.
Functions
Section titled “Functions”fn fnv1a
Section titled “fn fnv1a”pub
const fn fnv1a(bytes: &[u8]) -> u64Compute the FNV-1a 64-bit hash of a byte slice.
Source
pub const fn fnv1a(bytes: &[u8]) -> u64 { let mut hash = FNV_OFFSET_BASIS; let mut i = 0; while i < bytes.len() { hash ^= bytes[i] as u64; hash = hash.wrapping_mul(FNV_PRIME); i += 1; } hash}fn interface_hash
Section titled “fn interface_hash”pub
fn interface_hash(signatures: &[&str]) -> u64Compute the interface hash from a set of method signatures.
Signatures are sorted lexicographically before hashing to ensure
order-independence. Each signature is joined with \n as a separator.
This function is not const because it allocates for sorting.
The proc macro calls this at compile time via a build-script-like pattern,
or uses fnv1a directly on pre-sorted, concatenated signatures.
Source
pub fn interface_hash(signatures: &[&str]) -> u64 { let mut sorted: Vec<&str> = signatures.to_vec(); sorted.sort(); let combined = sorted.join("\n"); fnv1a(combined.as_bytes())}fn signature_string
Section titled “fn signature_string”pub
fn signature_string( name: &str, arg_types: &[String], ret: &str, wire_raw: bool, streaming: bool, client_streaming: bool,) -> StringBuild the canonical signature string for one method.
Format: "{name}:{arg_type_1},{arg_type_2}->{return_type}{!raw?}".
arg_typesare pre-stringified (typically bysyn::Type→to_token_stream().to_string()— the proc macro and any other tooling that wants to compute the same hash must use the same formatter).return_typeis the stringified return type, or empty string for methods returning().wire_raw = trueappends a trailing!rawmarker so methods opted into raw wire mode hash differently from bincode-typed methods of the same Rust signature. This is the protection that makes a wire-mode mismatch surface as a load-time hash mismatch instead of silent data corruption.streaming = trueappends a trailing!streammarker (after any!raw) so a server-streaming method (-> fidius::Stream<T>, whereretis the per-item typeT) hashes differently from a unary-> Tmethod of the same name/args. Same protection as!raw, for the streaming/unary split (FIDIUS-I-0026, D4).
This function lives in fidius-core (not fidius-macro) so the proc
macro and downstream tooling like fidius python-stub share a single
source of truth for the format. Drift between them = silent hash
mismatch, which is exactly what the load-time check is meant to catch
— but better to never have the drift in the first place.
Source
pub fn signature_string( name: &str, arg_types: &[String], ret: &str, wire_raw: bool, streaming: bool, client_streaming: bool,) -> String { let raw_marker = if wire_raw { "!raw" } else { "" }; let stream_marker = if streaming { "!stream" } else { "" }; // Client-streaming (`Stream<T>` in argument position) hashes distinctly from a // unary or server-streaming method of the same name/args (FIDIUS-I-0030 / ADR-0007). let client_stream_marker = if client_streaming { "<stream" } else { "" }; format!( "{}:{}->{}{}{}{}", name, arg_types.join(","), ret, raw_marker, stream_marker, client_stream_marker )}