pointer.go 873 B

12345678910111213141516171819202122232425262728293031323334
  1. // Copyright 2018, The Go 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. package value
  5. import (
  6. "reflect"
  7. "unsafe"
  8. )
  9. // Pointer is an opaque typed pointer and is guaranteed to be comparable.
  10. type Pointer struct {
  11. p unsafe.Pointer
  12. t reflect.Type
  13. }
  14. // PointerOf returns a Pointer from v, which must be a
  15. // reflect.Ptr, reflect.Slice, or reflect.Map.
  16. func PointerOf(v reflect.Value) Pointer {
  17. // The proper representation of a pointer is unsafe.Pointer,
  18. // which is necessary if the GC ever uses a moving collector.
  19. return Pointer{unsafe.Pointer(v.Pointer()), v.Type()}
  20. }
  21. // IsNil reports whether the pointer is nil.
  22. func (p Pointer) IsNil() bool {
  23. return p.p == nil
  24. }
  25. // Uintptr returns the pointer as a uintptr.
  26. func (p Pointer) Uintptr() uintptr {
  27. return uintptr(p.p)
  28. }