hash_str 0.0.3

Strings with a precomputed hash.
Documentation

hash_str

Strings with Precomputed Hash

A simple library for strings with a precomputed hash.

Features:

  • Create HashStr with precomputed hash
  • Create HashStrMap utilizing HashStr's precomputed hash
  • Intern strings into an explicit cache
  • Create HashStr at compile time with a macro, deduplicated

Wishlist:

  • Intern strings into a global cache like ustr

Non-Goals:

  • Dynamic string type like std String

Example:

use hash_str::hstr;
use hash_str::{HashStr,UnhashedStr};
// requires cache feature
use hash_str::{HashStrHost,HashStrCache};

fn main(){
	// string internment cache
	let lifetime_host=HashStrHost::new();
	let mut cache=HashStrCache::new();

	// string with hash calculated at compile time
	let hstr_static=hstr!("bruh");
	// string with hash calculated at run time
	// anonymous means it does not belong to any HashStrCache
	let hstr_runtime=&*HashStr::anonymous("bruh".to_owned());

	// intern string into deduplication cache
	// does not allocate unless "bruh" is a new string
	let hstr_interned=cache.intern_with(&lifetime_host,"bruh");

	let mut map=hash_str::HashStrMap::default();
	map.insert(hstr_static,1);

	assert_eq!(map.get(hstr_static),Some(&1));
	assert_eq!(map.get(hstr_runtime),Some(&1));
	assert_eq!(map.get(hstr_interned),Some(&1));
	// use Borrow<UnhashedStr> : &HashStr trait bound to index HashMap
	// without needing to allocate a temporary HashStr
	assert_eq!(map.get(UnhashedStr::from_ref("bruh")),Some(&1));

	// free cache memory of interned strings
	// does not affect static or anonymous HashStrs
	drop(cache);
	drop(lifetime_host);
}