safe

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package safe provides safe casting operations for different data types.

Index

Constants

View Source
const IntSize = 32 << (^uint(0) >> 63) // taken from strconv.IntSize and math.intSize

IntSize is the size in bits of an int or uint value on the running platform. Either 32 or 64.

Variables

View Source
var (
	// An integer overflow occurred.
	ErrIntegerOverflow = errors.New("integer overflow occurred")

	// An integer underflow occurred.
	ErrIntegerUnderflow = errors.New("integer underflow occurred")
)

Functions

func Add32

func Add32(x, y uint32) (uint32, error)

Add two unsigned 32bit integers. Returns ErrIntegerOverflow if an overflow occurred.

func Add64

func Add64(x, y uint64) (uint64, error)

Add two unsigned 64bit integers. Returns ErrIntegerOverflow if an overflow occurred.

func Int8ToUint8

func Int8ToUint8(x int8) (uint8, error)

Cast from a signed 8bit integer to an unsigned 8bit integer. Return ErrIntegerUnderflow if x contains a negative number.

func Int16ToUint16

func Int16ToUint16(x int16) (uint16, error)

Cast from a signed 16bit integer to an unsigned 16bit integer. Return ErrIntegerUnderflow if x contains a negative number.

func Int32ToUint32

func Int32ToUint32(x int32) (uint32, error)

Cast from a signed 32bit integer to an unsigned 32bit integer. Return ErrIntegerUnderflow if x contains a negative number.

func Int64ToInt32

func Int64ToInt32(x int64) (int32, error)

Downcast a signed 64bit integer to a signed 32bit integer. Returns ErrIntegerOverflow if an overflow occurred.

func Int64ToUint32

func Int64ToUint32(x int64) (uint32, error)

Downcast a signed 64bit integer to an unsigned 32bit integer. Returns ErrIntegerUnderflow if x is negative. Returns ErrIntegerOverflow if x is too big.

func Int64ToUint64

func Int64ToUint64(x int64) (uint64, error)

Cast from a signed 64bit integer to an unsigned 64bit integer. Return ErrIntegerUnderflow if x contains a negative number.

func IntToInt8

func IntToInt8(x int) (int8, error)

Cast from platform dependant signed integer to a signed 8bit integer. Return ErrIntegerUnderflow if x is too small. Return ErrIntegerOverflow if x is too big.

func IntToInt16

func IntToInt16(x int) (int16, error)

Cast from platform dependant signed integer to a signed 16bit integer. Return ErrIntegerUnderflow if x is too small. Return ErrIntegerOverflow if x is too big.

func IntToInt32

func IntToInt32(x int) (int32, error)

Cast from platform dependant signed integer to a signed 32bit integer. Return ErrIntegerUnderflow if x is too small. Return ErrIntegerOverflow if x is too big.

func IntToUint8

func IntToUint8(x int) (uint8, error)

Cast from platform dependant signed integer to an unsigned 8bit integer. Return ErrIntegerUnderflow if x is too small. Return ErrIntegerOverflow if x is too big.

func IntToUint16

func IntToUint16(x int) (uint16, error)

Cast from platform dependant signed integer to an unsigned 16bit integer. Return ErrIntegerUnderflow if x is too small. Return ErrIntegerOverflow if x is too big.

func IntToUint32

func IntToUint32(x int) (uint32, error)

Cast from platform dependant signed integer to an unsigned 32bit integer. Return ErrIntegerUnderflow if x is too small. Return ErrIntegerOverflow if x is too big.

func IntToUint64

func IntToUint64(x int) (uint64, error)

Cast from platform dependant signed integer to an unsigned 64bit integer. Return ErrIntegerUnderflow if x is too small.

func Sub32

func Sub32(x, y uint32) (uint32, error)

Subtract two unsigned 32bit integers. Returns ErrIntegerUnderflow if an underflow occurred.

func Sub64

func Sub64(x, y uint64) (uint64, error)

Subtract two unsigned 64bit integers. Returns ErrIntegerUnderflow if an underflow occurred.

func Uint8ToInt8

func Uint8ToInt8(x uint8) (int8, error)

Cast from an unsigned 8bit integer to a signed 8bit integer. Return ErrIntegerOverflow if x is too big.

func Uint16ToInt16

func Uint16ToInt16(x uint16) (int16, error)

Cast from an unsigned 16bit integer to a signed 16bit integer. Return ErrIntegerOverflow if x is too big.

func Uint32ToInt

func Uint32ToInt(x uint32) (int, error)

Cast from unsigned 32bit integer to platform dependant signed integer. Return ErrIntegerOverflow if x is too big.

func Uint32ToInt32

func Uint32ToInt32(x uint32) (int32, error)

Cast from an unsigned 32bit integer to a signed 32bit integer. Return ErrIntegerOverflow if x is too big.

func Uint64ToInt

func Uint64ToInt(x uint64) (int, error)

Cast from unsigned 64bit integer to platform dependant signed integer. Return ErrIntegerOverflow if x is too big.

func Uint64ToInt32

func Uint64ToInt32(x uint64) (int32, error)

Downcast an unsigned 64bit integer to a signed 32bit integer. Returns ErrIntegerOverflow if x is too big.

func Uint64ToInt64

func Uint64ToInt64(x uint64) (int64, error)

Cast from an unsigned 64bit integer to a signed 64bit integer. Return ErrIntegerOverflow if x is too big.

func Uint64ToUint32

func Uint64ToUint32(x uint64) (uint32, error)

Downcast an unsigned 64bit integer to an unsigned 32bit integer. Returns ErrIntegerOverflow if an overflow occurred.

func UintToUint8

func UintToUint8(x uint) (uint8, error)

Cast from platform dependant unsigned integer to an unsigned 8bit integer. Return ErrIntegerOverflow if x is too big.

func UintToUint16

func UintToUint16(x uint) (uint16, error)

Cast from platform dependant unsigned integer to an unsigned 16bit integer. Return ErrIntegerOverflow if x is too big.

func UintToUint32

func UintToUint32(x uint) (uint32, error)

Cast from platform dependant unsigned integer to an unsigned 32bit integer. Return ErrIntegerOverflow if x is too big.

Types

This section is empty.

Jump to

Keyboard shortcuts

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