NewHasher returns the built-in hasher for a given key type.
(key K)
| 2239 | |
| 2240 | // NewHasher returns the built-in hasher for a given key type. |
| 2241 | func 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. |
| 2261 | func hashString(value string) uint32 { |
no outgoing calls
searching dependent graphs…