mapiter_future.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build tailscale_go
  5. // +build tailscale_go
  6. package deephash
  7. import "reflect"
  8. // iterKey returns the current iter key.
  9. // scratch is a re-usable reflect.Value.
  10. // iterKey may store the iter key in scratch and return scratch,
  11. // or it may allocate and return a new reflect.Value.
  12. func iterKey(iter *reflect.MapIter, scratch reflect.Value) reflect.Value {
  13. iter.SetKey(scratch)
  14. return scratch
  15. }
  16. // iterVal returns the current iter val.
  17. // scratch is a re-usable reflect.Value.
  18. // iterVal may store the iter val in scratch and return scratch,
  19. // or it may allocate and return a new reflect.Value.
  20. func iterVal(iter *reflect.MapIter, scratch reflect.Value) reflect.Value {
  21. iter.SetValue(scratch)
  22. return scratch
  23. }
  24. // mapIter returns a map iterator for mapVal.
  25. // scratch is a re-usable reflect.MapIter.
  26. // mapIter may re-use scratch and return it,
  27. // or it may allocate and return a new *reflect.MapIter.
  28. // If mapVal is the zero reflect.Value, mapIter may return nil.
  29. func mapIter(scratch *reflect.MapIter, mapVal reflect.Value) *reflect.MapIter {
  30. scratch.Reset(mapVal) // always Reset, to allow the caller to avoid pinning memory
  31. if !mapVal.IsValid() {
  32. // Returning scratch would also be OK.
  33. // Do this for consistency with the non-optimized version.
  34. return nil
  35. }
  36. return scratch
  37. }