From 01d8bee14cd3d557bd6b4fcf6dfa1b23357def41 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 29 Feb 2024 17:29:33 +0200 Subject: [PATCH] lib/mergeset: use unsafe.Slice and unsafe.String instead of deprecated reflect.SliceHeader with unsafe conversion from slice header to string header --- lib/mergeset/encoding.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/mergeset/encoding.go b/lib/mergeset/encoding.go index 3e2feedb8..93429744d 100644 --- a/lib/mergeset/encoding.go +++ b/lib/mergeset/encoding.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "os" - "reflect" "sort" "strings" "sync" @@ -29,21 +28,15 @@ type Item struct { // The returned bytes representation belongs to data. func (it Item) Bytes(data []byte) []byte { n := int(it.End - it.Start) - sh := (*reflect.SliceHeader)(unsafe.Pointer(&data)) - sh.Cap = n - sh.Len = n - sh.Data += uintptr(it.Start) - return data + return unsafe.Slice((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(data)))+uintptr(it.Start))), n) } // String returns string representation of it obtained from data. // // The returned string representation belongs to data. func (it Item) String(data []byte) string { - sh := (*reflect.SliceHeader)(unsafe.Pointer(&data)) - sh.Data += uintptr(it.Start) - sh.Len = int(it.End - it.Start) - return *(*string)(unsafe.Pointer(sh)) + n := int(it.End - it.Start) + return unsafe.String((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(data)))+uintptr(it.Start))), n) } func (ib *inmemoryBlock) Len() int {