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