pointer_unsafe.go 911 B

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