github.com/chrislusf/greenpack@v3.7.1-0.20170911073826-ad5bd10b7c47+incompatible/msgp/unsafe.go (about) 1 // +build !appengine 2 3 package msgp 4 5 import ( 6 "reflect" 7 "unsafe" 8 ) 9 10 // NOTE: 11 // all of the definition in this file 12 // should be repeated in appengine.go, 13 // but without using unsafe 14 15 const ( 16 // spec says int and uint are always 17 // the same size, but that int/uint 18 // size may not be machine word size 19 smallint = unsafe.Sizeof(int(0)) == 4 20 ) 21 22 // UnsafeString returns the byte slice as a volatile string 23 // THIS SHOULD ONLY BE USED BY THE CODE GENERATOR. 24 // THIS IS EVIL CODE. 25 // YOU HAVE BEEN WARNED. 26 func UnsafeString(b []byte) string { 27 return *(*string)(unsafe.Pointer(&reflect.StringHeader{Data: uintptr(unsafe.Pointer(&b[0])), Len: len(b)})) 28 } 29 30 // UnsafeBytes returns the string as a byte slice 31 // THIS SHOULD ONLY BE USED BY THE CODE GENERATOR. 32 // THIS IS EVIL CODE. 33 // YOU HAVE BEEN WARNED. 34 func UnsafeBytes(s string) []byte { 35 return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ 36 Len: len(s), 37 Cap: len(s), 38 Data: (*(*reflect.StringHeader)(unsafe.Pointer(&s))).Data, 39 })) 40 }