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