Skip to content

Commit

Permalink
hack: remove deprecate SliceHeader (#47067)
Browse files Browse the repository at this point in the history
ref #47066
  • Loading branch information
hawkingrei authored Sep 19, 2023
1 parent 89fb7ad commit 1f7dcca
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions util/hack/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package hack

import (
"reflect"
"unsafe"
)

Expand All @@ -25,26 +24,17 @@ type MutableString string
// String converts slice to MutableString without copy.
// The MutableString can be converts to string without copy.
// Use it at your own risk.
func String(b []byte) (s MutableString) {
func String(b []byte) MutableString {
if len(b) == 0 {
return ""
}
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
pstring.Data = pbytes.Data
pstring.Len = pbytes.Len
return s
return MutableString(unsafe.String(unsafe.SliceData(b), len(b)))
}

// Slice converts string to slice without copy.
// Use at your own risk.
func Slice(s string) (b []byte) {
pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
pbytes.Data = pstring.Data
pbytes.Len = pstring.Len
pbytes.Cap = pstring.Len
return b
func Slice(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}

// LoadFactor is the maximum average load of a bucket that triggers growth is 6.5 in Golang Map.
Expand Down

0 comments on commit 1f7dcca

Please sign in to comment.