From e9403d8b182f2f94cc7fe775500dae43188bc211 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 30 Dec 2020 16:56:23 +0100 Subject: [PATCH] internal/unsafe: add StructView Needed by the compute renderer. Signed-off-by: Elias Naur --- internal/unsafe/unsafe.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/internal/unsafe/unsafe.go b/internal/unsafe/unsafe.go index 29c485fb..3e963777 100644 --- a/internal/unsafe/unsafe.go +++ b/internal/unsafe/unsafe.go @@ -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)