MCPcopy Create free account
hub / github.com/benbjohnson/immutable / NewHasher

Function NewHasher

immutable.go:2241–2258  ·  view source on GitHub ↗

NewHasher returns the built-in hasher for a given key type.

(key K)

Source from the content-addressed store, hash-verified

2239
2240// NewHasher returns the built-in hasher for a given key type.
2241func NewHasher[K any](key K) Hasher[K] {
2242 // Attempt to use non-reflection based hasher first.
2243 switch (any(key)).(type) {
2244 case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, string:
2245 return &defaultHasher[K]{}
2246 }
2247
2248 // Fallback to reflection-based hasher otherwise.
2249 // This is used when caller wraps a type around a primitive type.
2250 switch reflect.TypeOf(key).Kind() {
2251 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.String:
2252 return &reflectHasher[K]{}
2253 }
2254
2255 // If no hashers match then panic.
2256 // This is a compile time issue so it should not return an error.
2257 panic(fmt.Sprintf("immutable.NewHasher: must set hasher for %T type", key))
2258}
2259
2260// Hash returns a hash for value.
2261func hashString(value string) uint32 {

Callers 2

setMethod · 0.85
testNewHasherFunction · 0.85

Calls

no outgoing calls

Tested by 1

testNewHasherFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…