safe

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: MIT Imports: 2 Imported by: 4

README

Safe

Go Reference Go Report Card codecov

Purpose

Library that allows you to detect overflows in operations with integer numbers

Usage

Example:

package main

import (
    "fmt"

    "github.com/akramarenkov/safe"
)

func main() {
    sum, err := safe.SumInt[int8](3, 124)
    if err != nil {
        panic(err)
    }

    fmt.Println(sum)

    _, err = safe.SumInt[int8](3, 125)
    if err == nil {
        panic("expected overflow")
    }
    // Output: 127
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrValueOverflow = errors.New("value overflow")
)

Functions

func FloatToInt

func FloatToInt[
	Float constraints.Float,
	Integer constraints.Integer,
](float Float) (Integer, error)

func Invert

func Invert[Type constraints.Signed](number Type) (Type, error)

func IsMaxNegative

func IsMaxNegative[Type constraints.Integer](number Type) bool

func IsMaxPositive

func IsMaxPositive[Type constraints.Integer](number Type) bool

func PowUnsigned

func PowUnsigned[Type constraints.Unsigned](base Type, exponent Type) (Type, error)

func ProductInt

func ProductInt[Type constraints.Integer](first Type, second Type) (Type, error)

func SumInt

func SumInt[Type constraints.Integer](first Type, second Type) (Type, error)
Example
package main

import (
	"fmt"

	"github.com/akramarenkov/safe"
)

func main() {
	sum, err := safe.SumInt[int8](3, 124)
	if err != nil {
		panic(err)
	}

	fmt.Println(sum)

	_, err = safe.SumInt[int8](3, 125)
	if err == nil {
		panic("expected overflow")
	}
}
Output:

127

func UnsignedToSigned

func UnsignedToSigned[
	Unsigned constraints.Unsigned,
	Signed constraints.Signed,
](number Unsigned) (Signed, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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