Skip to content

Commit

Permalink
Use TypeParameter
Browse files Browse the repository at this point in the history
  • Loading branch information
lemon-mint committed Mar 20, 2022
1 parent 6be0be9 commit 0ce1bc6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 8 deletions.
10 changes: 3 additions & 7 deletions dumpstruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,15 @@ import (
// DumpStruct takes a structure instance v as input and copies the memory to dst.
//
// If the size of dst is smaller than the size of v, a new byte slice is allocated.
func DumpStruct(dst []byte, v interface{}) []byte {
srcV := reflect.ValueOf(v)

srcP := unsafe.Pointer(srcV.Pointer())

size := srcV.Elem().Type().Size()
func DumpStruct[T any](dst []byte, v *T) []byte {
size := int(unsafe.Sizeof(*v))
if len(dst) >= int(size) {
dst = dst[:size]
} else {
dst = make([]byte, size)
}
BufH := (*reflect.SliceHeader)(unsafe.Pointer(&dst))

MemMove(unsafe.Pointer(BufH.Data), srcP, size)
MemMove(unsafe.Pointer(BufH.Data), unsafe.Pointer(v), uintptr(size))
return dst
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/lemon-mint/libuseful

go 1.17
go 1.18

0 comments on commit 0ce1bc6

Please sign in to comment.