Skip to main content

Map

Type Alias Map 

Source
pub type Map<K, V> = HashMap<K, V>;
Expand description

The default owned HashMap type generated map<K, V> fields use.

On std builds this resolves to std::collections::HashMap<K, V, foldhash::fast::RandomState>; on no_std builds it is hashbrown::HashMap<K, V> (which defaults to the same foldhash hasher). This alias is the recommended way to name a generated map field’s type in downstream code, so that the concrete hasher and container stay an implementation detail.

Re-exported at the crate root as buffa::Map, alongside MapStorage: use buffa::Map;.

§Construction

Use Map::default to construct an empty map. The inherent HashMap::new() and HashMap::with_capacity() are only defined for the std default hasher, so they are unavailable on Map under std (and would be non-portable across the std/no_std boundary). For literals, use [(k, v), ..].into_iter().collect() rather than Map::from([...]) — From<[_; N]> is likewise hasher-pinned in std. To preallocate, use std::collections::HashMap::with_capacity_and_hasher(n, Default::default()).

§Hasher and HashDoS

foldhash::fast is per-instance seeded — each Map::default() derives a fresh seed from the stack pointer mixed with a thread-local counter, layered on a process-wide seed. On std that process-wide seed mixes ASLR addresses with the wall clock and an allocator address. The seed is not drawn from a CSPRNG (unlike std::hash::RandomState, which seeds SipHash from getrandom), and foldhash::fast does not advertise HashDoS resistance. Treat this default as not hardened against adversarial hash flooding.

On no_std it is weaker than that, in a way worth stating plainly. buffa gates foldhash/std behind its own std feature, and without it foldhash drops the clock and allocator contributions: the process-wide seed is then derived from three pointers alone — a stack address, a function address, and the address of a static. On a hosted target ASLR still varies those per process. On a bare-metal target such as thumbv7em-none-eabihf, which buffa supports and CI checks, there is no ASLR: all three are fixed at link time, so the seed is effectively a compile-time constant and a colliding key set can be computed once from the firmware image and reused against every device running it. If you decode map fields with attacker-controlled keys in a no_std build, prefer one of the alternatives below rather than this default.

This is the same trade-off Google’s protobuf-v4/upb makes (Wyhash). Consumers decoding map fields with attacker-controlled keys who need a hardened bound should select MapRepr::BTreeMap (no hashing, O(log n) worst case) or supply a SipHash-backed map via MapRepr::Custom. The MapStorage impl is generic over the hasher, so std::collections::HashMap<K, V, std::hash::RandomState> works without a newtype — only a foreign container type (e.g. IndexMap) needs one.

Aliased Type§

pub struct Map<K, V> { /* private fields */ }