Skip to content

fix: support pointer key types in hashKey - #84

Open
AdamMagued wants to merge 1 commit into
cornelk:mainfrom
AdamMagued:fix-support-pointer-key-types
Open

AdamMagued wants to merge 1 commit into
cornelk:mainfrom
AdamMagued:fix-support-pointer-key-types

Conversation

@AdamMagued

Copy link
Copy Markdown

Summary

In Go, native maps and sync.Map support pointer keys. In cornelk/hashmap, instantiating or hashing maps with pointer keys (*T or unsafe.Pointer) was unsupported because the hashable type constraint only allowed numeric and string types, and setDefaultHasher did not handle reflect.Pointer or reflect.UnsafePointer, causing a panic.

Changes

  • Updated the hashable interface constraint in defines.go to comparable, allowing pointer types while preserving compile-time comparability requirements.
  • Added reflect.Pointer and reflect.UnsafePointer to setDefaultHasher in util_hash.go, hashing the pointer address value using word-size xxhash (xxHashQword on 64-bit systems, xxHashDword on 32-bit systems).
  • Added regression tests in hashmap_test.go verifying insertion, retrieval, overwrite, deletion, and nil pointer handling for struct pointer keys (*struct{ name string }) and unsafe.Pointer keys.
  • Added unit tests in util_hash_test.go verifying pointer and unsafe.Pointer hashing behavior against the underlying word hasher.

Fixes #53

Support reflect.Pointer and reflect.UnsafePointer in setDefaultHasher by hashing the pointer address using word-size xxhash, and update the hashable type constraint to comparable.

Fixes cornelk#53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: unsupported key type *foo

1 participant