Added generic types to sizeof

This commit is contained in:
Eduard Urbach 2025-02-08 14:44:13 +01:00
parent b02b722542
commit d71bbd51cf
Signed by: eduard
GPG key ID: 49226B848C78F6C8
3 changed files with 8 additions and 6 deletions

View file

@ -3,15 +3,17 @@ package sizeof
import "math"
// Signed tells you how many bytes are needed to encode this signed number.
func Signed(number int64) int {
func Signed[T int | int8 | int16 | int32 | int64](number T) int {
x := int64(number)
switch {
case number >= math.MinInt8 && number <= math.MaxInt8:
case x >= math.MinInt8 && x <= math.MaxInt8:
return 1
case number >= math.MinInt16 && number <= math.MaxInt16:
case x >= math.MinInt16 && x <= math.MaxInt16:
return 2
case number >= math.MinInt32 && number <= math.MaxInt32:
case x >= math.MinInt32 && x <= math.MaxInt32:
return 4
default: