Documentation
¶
Overview ¶
Package intconv converts between integer types without silent wraparound: Exact refuses a value the target cannot hold, Must panics on one, Clamp saturates it, and Duration scales a count into a time.Duration. Uint32Bits, Uint64Bits and Int32Bits reinterpret two's-complement bits, where a wire format means exactly that.
Index ¶
- Variables
- func Clamp[T, F Integer](v F) T
- func Duration[F Integer](n F, unit time.Duration) time.Duration
- func Exact[T, F Integer](v F) (T, error)
- func Int32Bits(v uint32) int32
- func Must[T, F Integer](v F) T
- func Uint32Bits(v int32) uint32
- func Uint64Bits(v int64) uint64
- type Integer
- type RangeError
Constants ¶
This section is empty.
Variables ¶
var ErrRange = errors.New("integer out of range")
ErrRange is what every *RangeError unwraps to, for errors.Is.
Functions ¶
func Duration ¶
Duration is n units, saturated to time.Duration's range. It panics on a unit that is not positive.
func Int32Bits ¶
Int32Bits is the int32 with v's two's-complement bits, the value a wire format's signed 32-bit field carries in v: Uint32Bits's inverse.
func Must ¶
func Must[T, F Integer](v F) T
Must is v as T, for a value the caller has already bounded: a v T cannot hold is a bug in the caller, and panics with its *RangeError.
func Uint32Bits ¶
Uint32Bits is v's two's-complement bits, the word a reader taking it as an int32 finds v in.
func Uint64Bits ¶
Uint64Bits is v's two's-complement bits, the word a reader taking it as an int64 finds v in.
Types ¶
type Integer ¶
type Integer interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 |
~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr
}
Integer is every Go integer type.
type RangeError ¶
RangeError is a value its target type cannot hold.
func (*RangeError) Error ¶
func (e *RangeError) Error() string