converters

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package converters provides safe type conversion functions.

Index

Constants

This section is empty.

Variables

View Source
var ErrNegativeValue = ewrap.New("negative values cannot be converted to unsigned integers")

ErrNegativeValue is returned when attempting to convert a negative number to an unsigned type.

View Source
var ErrOverflow = ewrap.New("value overflows target type")

ErrOverflow is returned when a value exceeds the target type's bounds.

Functions

func SafeInt16FromInt64 added in v1.0.1

func SafeInt16FromInt64(value int64) (int16, error)

SafeInt16FromInt64 converts an int64 to int16, ensuring the value fits in the target type.

func SafeInt32FromInt64 added in v1.0.1

func SafeInt32FromInt64(value int64) (int32, error)

SafeInt32FromInt64 converts an int64 to int32, ensuring the value fits in the target type.

func SafeInt64FromUint64 added in v1.0.1

func SafeInt64FromUint64(value uint64) (int64, error)

SafeInt64FromUint64 converts a uint64 to int64, ensuring the value fits in the target type.

func SafeInt8FromInt64 added in v1.0.1

func SafeInt8FromInt64(value int64) (int8, error)

SafeInt8FromInt64 converts an int64 to int8, ensuring the value fits in the target type.

func SafeIntFromInt64

func SafeIntFromInt64(value int64) (int, error)

SafeIntFromInt64 converts an int64 to int, ensuring the value fits in the target type.

func SafeIntFromUint64 added in v1.0.1

func SafeIntFromUint64(value uint64) (int, error)

SafeIntFromUint64 converts a uint64 to int, ensuring the value fits in the target type.

func SafeUint16FromInt64 added in v1.0.1

func SafeUint16FromInt64(value int64) (uint16, error)

SafeUint16FromInt64 converts an int64 to uint16 while guarding against negative values and overflow.

func SafeUint32FromInt64 added in v1.0.1

func SafeUint32FromInt64(value int64) (uint32, error)

SafeUint32FromInt64 converts an int64 to uint32 while guarding against negative values and overflow.

func SafeUint32FromUint64 added in v1.0.1

func SafeUint32FromUint64(value uint64) (uint32, error)

SafeUint32FromUint64 converts a uint64 to uint32, ensuring the value fits in the target type.

func SafeUint64FromInt

func SafeUint64FromInt(value int) (uint64, error)

SafeUint64FromInt converts an int to uint64 while guarding against negative values and overflow.

func SafeUint64FromInt64

func SafeUint64FromInt64(value int64) (uint64, error)

SafeUint64FromInt64 converts an int64 to uint64 while guarding against negative values.

func SafeUint8FromInt64 added in v1.0.1

func SafeUint8FromInt64(value int64) (uint8, error)

SafeUint8FromInt64 converts an int64 to uint8 while guarding against negative values and overflow.

func SafeUintFromInt64 added in v1.0.1

func SafeUintFromInt64(value int64) (uint, error)

SafeUintFromInt64 converts an int64 to uint while guarding against negative values and overflow.

func ToInt added in v1.0.4

func ToInt[T Integer](value T) (int, error)

ToInt converts any integer type to int, ensuring the value fits in the target type.

func ToInt32 added in v1.0.4

func ToInt32[T Integer](value T) (int32, error)

ToInt32 converts any integer type to int32, ensuring the value fits in the target type.

func ToInt64 added in v1.0.4

func ToInt64[T Integer](value T) (int64, error)

ToInt64 converts any integer type to int64, guarding against overflow for unsigned inputs.

func ToUint added in v1.0.4

func ToUint[T Integer](value T) (uint, error)

ToUint converts any integer type to uint while guarding against negative values and overflow.

func ToUint32 added in v1.0.4

func ToUint32[T Integer](value T) (uint32, error)

ToUint32 converts any integer type to uint32 while guarding against negative values and overflow.

func ToUint64 added in v1.0.4

func ToUint64[T Integer](value T) (uint64, error)

ToUint64 converts any integer type to uint64 while guarding against negative values.

Types

type Integer added in v1.0.4

type Integer interface {
	Signed | Unsigned
}

Integer represents all integer types supported by the generic converters.

type Signed added in v1.0.4

type Signed interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

Signed represents signed integer types usable with generic converters.

type Unsigned added in v1.0.4

type Unsigned interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}

Unsigned represents unsigned integer types usable with generic converters.

Jump to

Keyboard shortcuts

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