redis/internal/unsafe.go

28 lines
521 B
Go
Raw Normal View History

2017-01-13 14:39:59 +03:00
// +build !appengine
package internal
import (
"reflect"
"unsafe"
)
func BytesToString(b []byte) string {
bytesHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b))
2017-06-02 16:19:43 +03:00
strHeader := reflect.StringHeader{
Data: bytesHeader.Data,
Len: bytesHeader.Len,
}
2017-01-13 14:39:59 +03:00
return *(*string)(unsafe.Pointer(&strHeader))
}
2017-06-02 16:19:43 +03:00
func StringToBytes(s string) []byte {
sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
bh := reflect.SliceHeader{
Data: sh.Data,
Len: sh.Len,
Cap: sh.Len,
}
return *(*[]byte)(unsafe.Pointer(&bh))
}