Documentation
¶
Overview ¶
Package unsafecast exposes functions to bypass the Go type system and perform conversions between types that would otherwise not be possible.
The functions of this package are mostly useful as optimizations to avoid memory copies when converting between compatible memory layouts; for example, casting a [][16]byte to a []byte in order to use functions of the standard bytes package on the slices.
With great power comes great responsibility.
Index ¶
- func AddressOf[T any](data []T) *T
- func AddressOfBytes(data []byte) *byte
- func AddressOfString(data string) *byte
- func BoolToBytes(data []bool) []byte
- func Bytes(data *byte, size int) []byte
- func BytesToBool(data []byte) []bool
- func BytesToFloat32(data []byte) []float32
- func BytesToFloat64(data []byte) []float64
- func BytesToInt16(data []byte) []int16
- func BytesToInt32(data []byte) []int32
- func BytesToInt64(data []byte) []int64
- func BytesToInt8(data []byte) []int8
- func BytesToString(data []byte) string
- func BytesToUint128(data []byte) [][16]byte
- func BytesToUint32(data []byte) []uint32
- func BytesToUint64(data []byte) []uint64
- func Float32ToBytes(data []float32) []byte
- func Float32ToUint32(data []float32) []uint32
- func Float64ToBytes(data []float64) []byte
- func Float64ToUint64(data []float64) []uint64
- func Int16ToBytes(data []int16) []byte
- func Int16ToUint16(data []int16) []uint16
- func Int32ToBytes(data []int32) []byte
- func Int32ToUint32(data []int32) []uint32
- func Int64ToBytes(data []int64) []byte
- func Int64ToUint64(data []int64) []uint64
- func Int8ToBytes(data []int8) []byte
- func PointerOf[T any](data []T) unsafe.Pointer
- func PointerOfString(data string) unsafe.Pointer
- func PointerOfValue(value reflect.Value) unsafe.Pointer
- func Slice[To, From any](data []From) []To
- func StringToBytes(data string) []byte
- func Uint128ToBytes(data [][16]byte) []byte
- func Uint32ToBytes(data []uint32) []byte
- func Uint32ToInt32(data []uint32) []int32
- func Uint32ToInt64(data []uint32) []int64
- func Uint64ToBytes(data []uint64) []byte
- func Uint64ToInt64(data []uint64) []int64
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddressOf ¶
func AddressOf[T any](data []T) *T
AddressOf returns the address to the first element in data, even if the slice has length zero.
func AddressOfBytes ¶
AddressOfBytes returns the address of the first byte in data.
func AddressOfString ¶
AddressOfString returns the address of the first byte in data.
func BoolToBytes ¶
func Bytes ¶
Bytes constructs a byte slice. The pointer to the first element of the slice is set to data, the length and capacity are set to size.
func BytesToBool ¶
func BytesToFloat32 ¶
func BytesToFloat64 ¶
func BytesToInt16 ¶
func BytesToInt32 ¶
func BytesToInt64 ¶
func BytesToInt8 ¶
func BytesToString ¶
BytesToString converts a byte slice to a string value. The returned string shares the backing array of the byte slice.
Programs using this function are responsible for ensuring that the data slice is not modified while the returned string is in use, otherwise the guarantee of immutability of Go string values will be violated, resulting in undefined behavior.
func BytesToUint128 ¶
func BytesToUint32 ¶
func BytesToUint64 ¶
func Float32ToBytes ¶
func Float32ToUint32 ¶
func Float64ToBytes ¶
func Float64ToUint64 ¶
func Int16ToBytes ¶
func Int16ToUint16 ¶
func Int32ToBytes ¶
func Int32ToUint32 ¶
func Int64ToBytes ¶
func Int64ToUint64 ¶
func Int8ToBytes ¶
func PointerOf ¶
PointerOf is like AddressOf but returns an unsafe.Pointer, losing type information about the underlying data.
func PointerOfString ¶
PointerOfString is like AddressOfString but returns an unsafe.Pointer, losing type information about the underlying data.
func PointerOfValue ¶
PointerOfValue returns the address of the object packed in the given value.
This function is like value.UnsafePointer but works for any underlying type, bypassing the safety checks done by the reflect package.
func Slice ¶
func Slice[To, From any](data []From) []To
Slice converts the data slice of type []From to a slice of type []To sharing the same backing array. The length and capacity of the returned slice are scaled according to the size difference between the source and destination types.
Note that the function does not perform any checks to ensure that the memory layouts of the types are compatible, it is possible to cause memory corruption if the layouts mismatch (e.g. the pointers in the From are different than the pointers in To).
func StringToBytes ¶
StringToBytes applies the inverse conversion of BytesToString.
func Uint128ToBytes ¶
func Uint32ToBytes ¶
func Uint32ToInt32 ¶
func Uint32ToInt64 ¶
func Uint64ToBytes ¶
func Uint64ToInt64 ¶
Types ¶
This section is empty.