Explorar el Código

add UnsafeCastString

Tao Wen hace 7 años
padre
commit
05fbef0ca5
Se han modificado 1 ficheros con 10 adiciones y 0 borrados
  1. 10 0
      reflect2.go

+ 10 - 0
reflect2.go

@@ -283,3 +283,13 @@ func NoEscape(p unsafe.Pointer) unsafe.Pointer {
 	x := uintptr(p)
 	return unsafe.Pointer(x ^ 0)
 }
+
+func UnsafeCastString(str string) []byte {
+	stringHeader := (*reflect.StringHeader)(unsafe.Pointer(&str))
+	sliceHeader := &reflect.SliceHeader{
+		Data: stringHeader.Data,
+		Cap: stringHeader.Len,
+		Len: stringHeader.Len,
+	}
+	return *(*[]byte)(unsafe.Pointer(sliceHeader))
+}