handle.go 645 B

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