internal/unsafe: add StructView

Needed by the compute renderer.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-12-30 16:56:23 +01:00
parent 269e7e0d7b
commit e9403d8b18
+12
View File
@@ -7,6 +7,18 @@ import (
"unsafe"
)
// StructView returns a byte slice view of a struct.
func StructView(s interface{}) []byte {
v := reflect.ValueOf(s).Elem()
sz := int(v.Type().Size())
var res []byte
h := (*reflect.SliceHeader)(unsafe.Pointer(&res))
h.Data = uintptr(unsafe.Pointer(v.UnsafeAddr()))
h.Cap = sz
h.Len = sz
return res
}
// BytesView returns a byte slice view of a slice.
func BytesView(s interface{}) []byte {
v := reflect.ValueOf(s)