cogs_gamedev/hash.rs
1//! Utilities for quick and dirty hashing.
2
3use ahash::AHasher;
4
5use std::hash::{Hash, Hasher};
6
7/**
8Get a hash for a value. This is handy for anytime you need a random-ish, but
9constant, value based on some other value.
10
11One good usecase is variagated tilesets:
12pass in the tile's [`ICoord`] position to this function
13and use it as a selector on the tile variants.
14
15This isn't guaranteed to be the same across compiles or restarts,
16but it will be the same for a given input value across one run of a program.
17
18```
19# use cogs_gamedev::hash::hashcode;
20
21assert_eq!(hashcode(&10i32), hashcode(&10i32));
22assert_ne!(hashcode(&10i32), hashcode(&600i32));
23
24```
25
26[`ICoord`]: crate::grids::ICoord
27*/
28pub fn hashcode<H: Hash + ?Sized>(hashee: &H) -> u64 {
29 let mut hasher = AHasher::default();
30 hashee.hash(&mut hasher);
31 hasher.finish()
32}