diff options
Diffstat (limited to 'lib/binstruct/size.go')
-rw-r--r-- | lib/binstruct/size.go | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/lib/binstruct/size.go b/lib/binstruct/size.go index 365da85..d6d70c6 100644 --- a/lib/binstruct/size.go +++ b/lib/binstruct/size.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> +// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com> // // SPDX-License-Identifier: GPL-2.0-or-later @@ -28,8 +28,16 @@ var ( unmarshalerType = reflect.TypeOf((*Unmarshaler)(nil)).Elem() ) +const ( + sizeof8 = 1 + sizeof16 = 2 + sizeof32 = 4 + sizeof64 = 8 +) + func staticSize(typ reflect.Type) (int, error) { if typ.Implements(staticSizerType) { + //nolint:forcetypeassert // Already did a type check via reflection. return reflect.New(typ).Elem().Interface().(StaticSizer).BinaryStaticSize(), nil } if typ.Implements(marshalerType) || typ.Implements(unmarshalerType) { @@ -42,13 +50,13 @@ func staticSize(typ reflect.Type) (int, error) { } switch typ.Kind() { case reflect.Uint8, reflect.Int8: - return 1, nil + return sizeof8, nil case reflect.Uint16, reflect.Int16: - return 2, nil + return sizeof16, nil case reflect.Uint32, reflect.Int32: - return 4, nil + return sizeof32, nil case reflect.Uint64, reflect.Int64: - return 8, nil + return sizeof64, nil case reflect.Ptr: return staticSize(typ.Elem()) case reflect.Array: |