mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-07 18:35:34 +00:00
internal/unsafe: add StructView
Needed by the compute renderer. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -7,6 +7,18 @@ import (
|
|||||||
"unsafe"
|
"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.
|
// BytesView returns a byte slice view of a slice.
|
||||||
func BytesView(s interface{}) []byte {
|
func BytesView(s interface{}) []byte {
|
||||||
v := reflect.ValueOf(s)
|
v := reflect.ValueOf(s)
|
||||||
|
|||||||
Reference in New Issue
Block a user