reflect: add Value.UnsafePointer method

This was added in Go 1.18.
This commit is contained in:
Ayke van Laethem
2022-06-11 17:51:57 +02:00
committed by Ayke
parent bb65c5ce2b
commit caf405b01d
3 changed files with 14 additions and 8 deletions
+10 -4
View File
@@ -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"})
}
}