unsafecast

package
v0.0.0-...-3dde240 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

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

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

func AddressOfBytes(data []byte) *byte

AddressOfBytes returns the address of the first byte in data.

func AddressOfString

func AddressOfString(data string) *byte

AddressOfString returns the address of the first byte in data.

func BoolToBytes

func BoolToBytes(data []bool) []byte

func Bytes

func Bytes(data *byte, size int) []byte

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 BytesToBool(data []byte) []bool

func BytesToFloat32

func BytesToFloat32(data []byte) []float32

func BytesToFloat64

func BytesToFloat64(data []byte) []float64

func BytesToInt16

func BytesToInt16(data []byte) []int16

func BytesToInt32

func BytesToInt32(data []byte) []int32

func BytesToInt64

func BytesToInt64(data []byte) []int64

func BytesToInt8

func BytesToInt8(data []byte) []int8

func BytesToString

func BytesToString(data []byte) string

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 BytesToUint128(data []byte) [][16]byte

func BytesToUint32

func BytesToUint32(data []byte) []uint32

func BytesToUint64

func BytesToUint64(data []byte) []uint64

func Float32ToBytes

func Float32ToBytes(data []float32) []byte

func Float32ToUint32

func Float32ToUint32(data []float32) []uint32

func Float64ToBytes

func Float64ToBytes(data []float64) []byte

func Float64ToUint64

func Float64ToUint64(data []float64) []uint64

func Int16ToBytes

func Int16ToBytes(data []int16) []byte

func Int16ToUint16

func Int16ToUint16(data []int16) []uint16

func Int32ToBytes

func Int32ToBytes(data []int32) []byte

func Int32ToUint32

func Int32ToUint32(data []int32) []uint32

func Int64ToBytes

func Int64ToBytes(data []int64) []byte

func Int64ToUint64

func Int64ToUint64(data []int64) []uint64

func Int8ToBytes

func Int8ToBytes(data []int8) []byte

func PointerOf

func PointerOf[T any](data []T) unsafe.Pointer

PointerOf is like AddressOf but returns an unsafe.Pointer, losing type information about the underlying data.

func PointerOfString

func PointerOfString(data string) unsafe.Pointer

PointerOfString is like AddressOfString but returns an unsafe.Pointer, losing type information about the underlying data.

func PointerOfValue

func PointerOfValue(value reflect.Value) unsafe.Pointer

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

func StringToBytes(data string) []byte

StringToBytes applies the inverse conversion of BytesToString.

func Uint128ToBytes

func Uint128ToBytes(data [][16]byte) []byte

func Uint32ToBytes

func Uint32ToBytes(data []uint32) []byte

func Uint32ToInt32

func Uint32ToInt32(data []uint32) []int32

func Uint32ToInt64

func Uint32ToInt64(data []uint32) []int64

func Uint64ToBytes

func Uint64ToBytes(data []uint64) []byte

func Uint64ToInt64

func Uint64ToInt64(data []uint64) []int64

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL