Skip to content

Commit

Permalink
reflect: add Value.UnsafePointer method
Browse files Browse the repository at this point in the history
This was added in Go 1.18.
  • Loading branch information
aykevl committed Jun 11, 2022
1 parent bb65c5c commit caf405b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/reflect/deepequal.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func deepValueEqual(v1, v2 Value, visited map[visit]struct{}) bool {
if v1.Len() != v2.Len() {
return false
}
if v1.Pointer() == v2.Pointer() {
if v1.UnsafePointer() == v2.UnsafePointer() {
return true
}
for i := 0; i < v1.Len(); i++ {
Expand All @@ -91,7 +91,7 @@ func deepValueEqual(v1, v2 Value, visited map[visit]struct{}) bool {
}
return deepValueEqual(v1.Elem(), v2.Elem(), visited)
case Ptr:
if v1.Pointer() == v2.Pointer() {
if v1.UnsafePointer() == v2.UnsafePointer() {
return true
}
return deepValueEqual(v1.Elem(), v2.Elem(), visited)
Expand All @@ -109,7 +109,7 @@ func deepValueEqual(v1, v2 Value, visited map[visit]struct{}) bool {
if v1.Len() != v2.Len() {
return false
}
if v1.Pointer() == v2.Pointer() {
if v1.UnsafePointer() == v2.UnsafePointer() {
return true
}
for _, k := range v1.MapKeys() {
Expand Down
14 changes: 10 additions & 4 deletions src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,22 @@ func (v Value) IsNil() bool {
// Pointer returns the underlying pointer of the given value for the following
// types: chan, map, pointer, unsafe.Pointer, slice, func.
func (v Value) Pointer() uintptr {
return uintptr(v.UnsafePointer())
}

// UnsafePointer returns the underlying pointer of the given value for the
// following types: chan, map, pointer, unsafe.Pointer, slice, func.
func (v Value) UnsafePointer() unsafe.Pointer {
switch v.Kind() {
case Chan, Map, Ptr, UnsafePointer:
return uintptr(v.pointer())
return v.pointer()
case Slice:
slice := (*sliceHeader)(v.value)
return uintptr(slice.data)
return slice.data
case Func:
panic("unimplemented: (reflect.Value).Pointer()")
panic("unimplemented: (reflect.Value).UnsafePointer()")
default: // not implemented: Func
panic(&ValueError{Method: "Pointer"})
panic(&ValueError{Method: "UnsafePointer"})
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func reflectValueEqual(x, y reflect.Value) bool {
case reflect.String:
return x.String() == y.String()
case reflect.Chan, reflect.Ptr, reflect.UnsafePointer:
return x.Pointer() == y.Pointer()
return x.UnsafePointer() == y.UnsafePointer()
case reflect.Array:
for i := 0; i < x.Len(); i++ {
if !reflectValueEqual(x.Index(i), y.Index(i)) {
Expand Down

0 comments on commit caf405b

Please sign in to comment.