mirror of
https://github.com/fumiama/go-registry.git
synced 2026-06-07 17:00:27 +08:00
31 lines
729 B
Go
31 lines
729 B
Go
package registry
|
|
|
|
import "unsafe"
|
|
|
|
// slice is the runtime representation of a slice.
|
|
// It cannot be used safely or portably and its representation may
|
|
// change in a later release.
|
|
//
|
|
// Unlike reflect.SliceHeader, its Data field is sufficient to guarantee the
|
|
// data it references will not be garbage collected.
|
|
type slice struct {
|
|
Data unsafe.Pointer
|
|
Len int
|
|
Cap int
|
|
}
|
|
|
|
// BytesToString 没有内存开销的转换
|
|
func BytesToString(b []byte) string {
|
|
return *(*string)(unsafe.Pointer(&b))
|
|
}
|
|
|
|
// StringToBytes 没有内存开销的转换
|
|
func StringToBytes(s string) (b []byte) {
|
|
bh := (*slice)(unsafe.Pointer(&b))
|
|
sh := (*slice)(unsafe.Pointer(&s))
|
|
bh.Data = sh.Data
|
|
bh.Len = sh.Len
|
|
bh.Cap = sh.Len
|
|
return b
|
|
}
|