pointer_unsafe.go 892 B

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