set.go 679 B

1234567891011121314151617181920212223242526272829
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Package set contains set types.
  4. package set
  5. // HandleSet is a set of T.
  6. //
  7. // It is not safe for concurrent use.
  8. type HandleSet[T any] map[Handle]T
  9. // Handle is a opaque comparable value that's used as the map key
  10. // in a HandleSet. The only way to get one is to call HandleSet.Add.
  11. type Handle struct {
  12. v *byte
  13. }
  14. // Add adds the element (map value) e to the set.
  15. //
  16. // It returns the handle (map key) with which e can be removed, using a map
  17. // delete.
  18. func (s *HandleSet[T]) Add(e T) Handle {
  19. h := Handle{new(byte)}
  20. if *s == nil {
  21. *s = make(HandleSet[T])
  22. }
  23. (*s)[h] = e
  24. return h
  25. }