From fdfa48108286508caaebb253ed138c201a03cc54 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 11 Mar 2021 11:40:41 +0100 Subject: [PATCH] internal/byteslice: add specialized Uint32 for []uint32 view Uint32 is the garbage-free and more efficient version of Slice. Signed-off-by: Elias Naur --- internal/byteslice/byteslice.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/byteslice/byteslice.go b/internal/byteslice/byteslice.go index 71bc7047..26ebdb28 100644 --- a/internal/byteslice/byteslice.go +++ b/internal/byteslice/byteslice.go @@ -21,6 +21,16 @@ func Struct(s interface{}) []byte { return res } +// Uint32 returns a byte slice view of a uint32 slice. +func Uint32(s []uint32) []byte { + n := len(s) + if n == 0 { + return nil + } + blen := n * int(unsafe.Sizeof(s[0])) + return (*[1 << 30]byte)(unsafe.Pointer(&s[0]))[:blen:blen] +} + // Slice returns a byte slice view of a slice. func Slice(s interface{}) []byte { v := reflect.ValueOf(s)