mapiter_future.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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. // +build tailscale_go
  5. package deephash
  6. import "reflect"
  7. // iterKey returns the current iter key.
  8. // scratch is a re-usable reflect.Value.
  9. // iterKey may store the iter key in scratch and return scratch,
  10. // or it may allocate and return a new reflect.Value.
  11. func iterKey(iter *reflect.MapIter, scratch reflect.Value) reflect.Value {
  12. iter.SetKey(scratch)
  13. return scratch
  14. }
  15. // iterVal returns the current iter val.
  16. // scratch is a re-usable reflect.Value.
  17. // iterVal may store the iter val in scratch and return scratch,
  18. // or it may allocate and return a new reflect.Value.
  19. func iterVal(iter *reflect.MapIter, scratch reflect.Value) reflect.Value {
  20. iter.SetValue(scratch)
  21. return scratch
  22. }
  23. // mapIter returns a map iterator for mapVal.
  24. // scratch is a re-usable reflect.MapIter.
  25. // mapIter may re-use scratch and return it,
  26. // or it may allocate and return a new *reflect.MapIter.
  27. // If mapVal is the zero reflect.Value, mapIter may return nil.
  28. func mapIter(scratch *reflect.MapIter, mapVal reflect.Value) *reflect.MapIter {
  29. scratch.Reset(mapVal) // always Reset, to allow the caller to avoid pinning memory
  30. if !mapVal.IsValid() {
  31. // Returning scratch would also be OK.
  32. // Do this for consistency with the non-optimized version.
  33. return nil
  34. }
  35. return scratch
  36. }